19,502
社区成员
发帖
与我相关
我的任务
分享
/这个是连接数据库的,相关的Sql的dll文件我也都拷贝到项目中了。
private void button1_Click(object sender, EventArgs e)
{
string connectionString = GetConnectionString();
string queryString = "SELECT * FROM BBB";
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand command = connection.CreateCommand();
command.CommandText = queryString;
try
{
//MessageBox.Show("马上进行连接");
connection.Open();
MessageBox.Show("连接成功");
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
//Console.WriteLine("\t{0}\t{1}", reader[0], reader[1]);
MessageBox.Show(Convert.ToString(reader[0]));
}
reader.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
static private string GetConnectionString()
{
//return "Data Source=192.168.0.55;Initial Catalog=EY_TCCSFXT;User ID=sa;Password=;";
//return "Data Source=192.168.0.55;Database=EY_TCCSFXT;UID=sa;pwd=;";
return "server=EY812B;uid=sa;pwd=;database=EY_TCCSFXT;";
//return "Data Source=192.168.0.55;Initial Catalog=EY_TCCSFXT;Persist Security Info=True;User ID=sa;Password=";
}private void button1_Click(object sender, EventArgs e)
{
string connectionString = GetConnectionString();
string queryString = "SELECT * FROM BBB";
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand command = connection.CreateCommand();
command.CommandText = queryString;
try
{
//MessageBox.Show("马上进行连接");
connection.Open();
MessageBox.Show("连接成功");
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
//Console.WriteLine("\t{0}\t{1}", reader[0], reader[1]);
MessageBox.Show(Convert.ToString(reader[0]));
}
reader.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
static private string GetConnectionString()
{
//return "Data Source=192.168.0.55;Initial Catalog=EY_TCCSFXT;User ID=sa;Password=;";
//return "Data Source=192.168.0.55;Database=EY_TCCSFXT;UID=sa;pwd=;";
return "server=EY812B;uid=sa;pwd=;database=EY_TCCSFXT;";
//return "Data Source=192.168.0.55;Initial Catalog=EY_TCCSFXT;Persist Security Info=True;User ID=sa;Password=";
}
//这个是通过wifi,收发信号的,也不好使
private void btnConSvr_Click(object sender, EventArgs e)
{
try
{
btnConSvr.Enabled = false;
btnCloseConSvr.Enabled = true;
Client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress ipAddress = IPAddress.Parse(txtIPAddr.Text);
int port = int.Parse(txtPort.Text);
IPEndPoint remoteEP = new IPEndPoint(ipAddress, port);
Client.BeginConnect(remoteEP, new AsyncCallback(ConnectCallback), Client);
//allDone.Reset();
statusBar1.Text = "操作状态:连接服务器成功!";
//播放正确的声音
PlaySound.Play(OkSoundPath);
Receive();
string c = ipAddress.ToString();
XmlUtils.SaveXmlFile(c);
//receiveDone.WaitOne();
}
catch (Exception ex)
{
ConSvrStatus = false;
btnConSvr.Enabled = true;
btnCloseConSvr.Enabled = true;
statusBar1.Text = "操作状态:连接服务器失败!";
//播放错误的声音
PlaySound.Play(ErrorSoundPath);
return;
}
}
private void button1_Click(object sender, EventArgs e)
{
//string connectionString = GetConnectionString();
string str1=@"Data Source=192.168.0.55,1433;Initial Catalog=EY_TCCSFXT;User ID=sa;Password=";
string queryString = "SELECT * FROM BBB";
using (SqlConnection connection = new SqlConnection(str1))
{
SqlCommand command = connection.CreateCommand();
command.CommandText = queryString;
//try
//{
//MessageBox.Show("马上进行连接");
connection.Open();
if (connection == null)
MessageBox.Show("连接服务器失败,请检查网络!");
else
MessageBox.Show("连接成功");
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
//Console.WriteLine("\t{0}\t{1}", reader[0], reader[1]);
MessageBox.Show(Convert.ToString(reader[0]));
}
reader.Close();
//}
//catch (Exception ex)
// {
// Console.WriteLine(ex.Message);
// }
}
}
string M_str_sqlcon = @"Data Source=192.168.1.106,1433;Initial Catalog=pubs;User ID=sa;Password=111111";
sqlcon = new SqlConnection(M_str_sqlcon);
sqlcon.Open();
//查询到数据退出,不执行入库操作
if (sqlcon == null)
{
MessageBox.Show("连接服务器失败,请检查网络!");
}
else
{
MessageBox.Show("连接服务成功");
}