///发送按钮 /// /// /// privatevoidbutton2_Click(objectsender,EventArgse) { try { IPEndPointclient=(IPEndPoint)this.comboBox1.SelectedItem; if(client==null) { MessageBox.Show("没有选中任何在线客户端!"); return; } if(!this.server.clientCheck(client)) { MessageBox.Show("目标客户端不在线!"); return; } server.sendMessage(client,textBox_msg.Text); } catch(ExceptionEx){MessageBox.Show(Ex.Message);} } /// ///下面显示的 /// /// /// privatevoidshow(IPEndPointipEndPoint,stringstr) { label_zt.Text=ipEndPoint.ToString()":"str; label_all.Text="当前在线人数:"this.server.ClientNumber.ToString(); } /// ///当组合框按下的时候 /// /// /// privatevoidcomboBox1_DropDown_1(objectsender,EventArgse) { try { Listlist=this.server.ClientAll; this.comboBox1.DataSource=list; } catch{} } /// ///关闭客户端的按钮 /// /// /// privatevoidbutton3_Click_1(objectsender,EventArgse) { IPEndPointclient=(IPEndPoint)this.comboBox1.SelectedItem; if(client==null) { MessageBox.Show("没有选中任何在线客户端!"); return; } if(!this.server.clientCheck(client)) { MessageBox.Show("目标客户端不在线!"); return; } server.clientClose(client); } #endregion-IMDN开发者社群-imdn.cn">
///发送按钮 /// /// /// privatevoidbutton2_Click(objectsender,EventArgse) { try { IPEndPointclient=(IPEndPoint)this.comboBox1.SelectedItem; if(client==null) { MessageBox.Show("没有选中任何在线客户端!"); return; } if(!this.server.clientCheck(client)) { MessageBox.Show("目标客户端不在线!"); return; } server.sendMessage(client,textBox_msg.Text); } catch(ExceptionEx){MessageBox.Show(Ex.Message);} } /// ///下面显示的 /// /// /// privatevoidshow(IPEndPointipEndPoint,stringstr) { label_zt.Text=ipEndPoint.ToString()":"str; label_all.Text="当前在线人数:"this.server.ClientNumber.ToString(); } /// ///当组合框按下的时候 /// /// /// privatevoidcomboBox1_DropDown_1(objectsender,EventArgse) { try { Listlist=this.server.ClientAll; this.comboBox1.DataSource=list; } catch{} } /// ///关闭客户端的按钮 /// /// /// privatevoidbutton3_Click_1(objectsender,EventArgse) { IPEndPointclient=(IPEndPoint)this.comboBox1.SelectedItem; if(client==null) { MessageBox.Show("没有选中任何在线客户端!"); return; } if(!this.server.clientCheck(client)) { MessageBox.Show("目标客户端不在线!"); return; } server.clientClose(client); } #endregion - IMDN开发者社群-imdn.cn">
首页 »
C# »
C#实现Socket通讯Demo
于 2021-08-22 发布
0 135
c#通过Socket实现图片、文件和文字的通讯。
private void button1_Click(object sender, EventArgs e)
{
try
{
server = TxStart.startServer(int.Parse(textBox_port.Text));
server.AcceptString = new TxDelegate(acceptString);
server.AcceptByte = new TxDelegate(acceptBytes);
server.Connect = new TxDelegate(connect);
server.dateSuccess = new TxDelegate(dateSuccess);
server.Disconnection = new TxDelegate(disconnection);
server.EngineClose = new TxDelegate(engineClose);
server.EngineLost = new TxDelegate(engineLost);
//server.BufferSize=12048;
//server.FileLog = "C:\test.txt";
server.StartEngine();
this.button1.Enabled = false;
this.button2.Enabled = true;
this.button3.Enabled = true;
}
catch (Exception Ex) { MessageBox.Show(Ex.Message); }
}
///
/// 发送按钮
///
///
///
private void button2_Click(object sender, EventArgs e)
{
try
{
IPEndPoint client = (IPEndPoint)this.comboBox1.SelectedItem;
if (client == null)
{
MessageBox.Show("没有选中任何在线客户端!");
return;
}
if (!this.server.clientCheck(client))
{
MessageBox.Show("目标客户端不在线!");
return;
}
server.sendMessage(client, textBox_msg.Text);
}
catch (Exception Ex) { MessageBox.Show(Ex.Message); }
}
///
/// 下面显示的
///
///
///
private void show(IPEndPoint ipEndPoint, string str)
{
label_zt.Text = ipEndPoint.ToString() ":" str;
label_all.Text = "当前在线人数:" this.server.ClientNumber.ToString();
}
///
/// 当组合框按下的时候
///
///
///
private void comboBox1_DropDown_1(object sender, EventArgs e)
{
try
{
List list = this.server.ClientAll;
this.comboBox1.DataSource = list;
}
catch { }
}
///
/// 关闭客户端的按钮
///
///
///
private void button3_Click_1(object sender, EventArgs e)
{
IPEndPoint client = (IPEndPoint)this.comboBox1.SelectedItem;
if (client == null)
{
MessageBox.Show("没有选中任何在线客户端!");
return;
}
if (!this.server.clientCheck(client))
{
MessageBox.Show("目标客户端不在线!");
return;
}
server.clientClose(client);
}
#endregion
下载说明:请别用迅雷下载,失败请重下,重下不扣分!
-
C#连接数据库(SQL Server,MySQL,Microsoft Access,DB2,Oracle)连接字符串
C#/.NET项目开发过程中,特别是中大型的项目,跟数据库打交道那是再正常不过的事情了。数据库可以帮助我们保存和管理数据。C#/.NET的项目中,我们也可以使用不同的数据库。
那么,我们今天就来总结整理一下C#/.NET的连接各类主流数据库(SQL Server,MySQL,Microsoft Access,DB2,Oracle)的连接字符串。
一、C#/.NET连接Microsoft SQL Server
1. 使用ODBC数据库连接
// ODBC标准连接
using System.Data.Odbc;
OdbcConnection conn = new OdbcConnection();
conn.ConnectionString = "Driver={SQL Server}; Server=ServerName; DataBase=DataBaseName; Uid=UserName; Pwd=password";
conn.Open();
// ODBC受信任的连接
using System.Data.Odbc;
OdbcConnection conn = new OdbcConnection();
conn.ConnectionString = "Driver={SQL Server}; Server=ServerName; DataBase=DataBaseName; Uid=admin; Pwd=password";
conn.Open();
// 或者
OdbcConnection conn = new OdbcConnection();
conn.ConnectionString = "Driver={SQL Server}; Server=ServerName; DataBase=DataBaseName; Trusted_Connection=Yes;";
2.使用OLEDB
// OleDb标准连接
using System.Data.OleDb;
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = "Driver=SQLOLEDB; Data Source=ServerName; Initial Catalog=DataBaseName; User id=UserName; Password=Secret;";
conn.Open();
// OleDb受信任的连接
using System.Data.OleDb;
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = "Driver=SQLOLEDB; Data Source=ServerName; Initial Catalog=DataBaseName; Integrated Security=SSPI;";
conn.Open();
3.使用.Net DataProvider
// .NET DataProvider标准连接
using System.Data.SqlClient;
SqlConnection conn = new SqlDbConnection();
conn.ConnectionString ="Data Source=ServerName; Initial Catalog=DataBaseName; User id=UserName; Password=Secret;";
conn.Open();
Data Source=DESKTOP-AC9DHQ6;Initial Catalog=MyKTV;Persist Security Info=True;User ID=sa;Password=mdg456123
// .NET DataProvider受信任连接
using System.Data.SqlClient;
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source=ServerName; Initial Catalog=DataBaseName; Integrated Security=SSPI;";
conn.Open();
二、C#/.NET连接Microsoft Access
1.使用ODBC连接
// ODBC标准连接
using System.Data.Odbc;
OdbcConnection conn = new OdbcConnection();
conn.ConnectionString = "Driver={Microsoft Access Driver (*.mdb)}; Dbq=c:myPathmyDb.mdb; Uid=Admin; Pwd=;password";
conn.Open();
// ODBC工作组(Workgroup)连接
using System.Data.Odbc;
OdbcConnection conn = new OdbcConnection();
conn.ConnectionString = "Driver={Microsoft Access Driver (*.mdb)}; Dbq=c:myPathmyDb.mdb; SystemDb=c:myPathmyDb.mdw;";
conn.Open();
2.使用OLEDB连接
// 使用OleDb的Jet标准安全连接
using System.Data.OleDb;
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:mypathmyDb.mdb; User id=admin;Password=password";
conn.Open();
// 使用OleDb的Jet System Database连接
using System.Data.OleDb;
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:mypathmyDb.mdb; System Database=c:mypathmyDb.mdw";
conn.Open();
三、C#/.NET连接MySQL数据库
1.使用ODBC数据源连接
// ODBC使用MyODBC Driver的远程数据库连接
using System.Data.Odbc;
OdbcConnection conn = new OdbcConnection();
conn.ConnectionString = "Driver={MySql}; Server=192.168.0.10; Option=131072; Port=3306; Stmt=; DataBase=DataBaseName; Uid=UserName; Pwd=password;" ;
conn.Open();
2.使用OLEDB连接
// OleDb标准连接
using System.Data.OleDb;
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = "Provider=MySqlProv; Data Source=ServerName; User id=UserName; Password=Password";
conn.Open();
四、C#/.NET连接DB2数据库
1.使用ODBC连接
// 不带DSN的ODBC连接
using System.Data.Odbc;
OdbcConnection conn = new OdbcConnection();
conn.ConnectionString = "Driver={IBM DB2 ODBC DRIVER};DataBase=DataBaseName; HostName=ServerName; Protocol=TCPIP;Port=PortNumber;Uid=UserName;Pwd=Secret";
conn.Open();
2.使用OLEDB连接
// OleDb微软官方驱动
using System.Data.OleDb;
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = "Driver=DB2OLEDB; Network Transport Library=TCPIP; Network Address=xxx.xxx.xxx.xxx; Package Collection=CollectionName; Initial Catalog=DataBaseName; User id=UserName; Password=Secret;";
conn.Open();
// OleDb IBM 驱动
using System.Data.OleDb;
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = "Driver=IBMDADB2; DataBase=DataBaseName; HostName=ServerName; Protocol=TCPIP; Port=PortNumber; Uid=UserName; Pwd=Secret;";
conn.Open();
3.使用.Net DataProvider第三方驱动
// 来自IBM的.NET DataProvider
using IBM.Data.DB2;
Db2Connection conn = new Db2Connection();
conn.ConnectionString = "DataBase=DataBaseName;Uid=UserName;Pwd=Secret";
conn.Open();
五、C#/.NET连接Oracle数据库
1.使用ODBC连接
// ODBC 微软的官方驱动
using System.Data.Odbc;
OdbcConnection conn = new OdbcConnection();
conn.ConnectionString = "Driver={Microsoft ODBC for Oracle};Server=OracleServer.world;Uid=UserName;Pwd=Secret;";
conn.Open();
// ODBC Oracle的驱动
using System.Data.Odbc;
OdbcConnection conn = new OdbcConnection();
conn.ConnectionString = "Driver={Oracle ODBC Driver};Dbq=myDataBase;Uid=UserName;Pwd=Secret;";
conn.Open();
2.使用OLEDB连接
// OleDb:Oracle驱动的标准连接
using System.Data.OleDb;
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = "Driver=OraOLEDB.Oracle;Data Source=ServerName;User id=UserName;Password=Secret;";
conn.Open();
// OleD:Oracle驱动的受信任连接
using System.Data.OleDb;
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = "Driver=OraOLEDB.Oracle;Data Source=ServerName;OSAuthent=1;";
conn.Open();
// 或者写成
using System.Data.OleDb;
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = "Driver=OraOLEDB.Oracle;Data Source=ServerName;User id=admin;Password=pwd";
conn.Open();
- 2021-08-07 00:31:00下载
- 积分:1
-
C# 柱状图、折线图、饼图源码(chart控件的简单示例)
C# 柱状图、折线图、饼图源码(chart控件的简单示例)
- 2015-06-11下载
- 积分:1
-
STM32远程升级源码(包括上位机)
【实例简介】
- 2021-06-02 00:31:33下载
- 积分:1
-
纸牌游戏21点 源码下载
一个不错的学习例子,希望可以帮到正在研究做游戏的朋友们,注意哦,是C#开发的.
- 2014-05-09下载
- 积分:1
-
pwm测试模块 dsp
【实例简介】pwm测试模块 dsp
ti芯片 测试用例 直接可以使用
- 2021-12-11 00:41:45下载
- 积分:1
-
C#扫代理IP
C#扫描代理IP
- 2015-01-06下载
- 积分:1
-
jQuery完全模拟windows系统桌面操作效果
jQuery完全模拟windows系统桌面操作效果
- 2015-05-01下载
- 积分:1
-
最小覆盖圆问题(c++代码)
:输入N个点的坐标,求一个能覆盖全部点 的最小圆
- 2020-05-24下载
- 积分:1
-
linux 屏幕操作
【实例简介】
- 2021-10-13 00:31:05下载
- 积分:1
-
C语言函数大全(语法着色)html版本
C语言函数大全(语法着色)html版本
- 2021-05-06下载
- 积分:1