c急剧的数据库连接问题 [英] c sharp database connection problem
本文介绍了c急剧的数据库连接问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有关于数据库连接的问题。请帮助我这方面
代码是
i have problem about database connectivity.please help me this regard
code is
private void button5_Click(object sender, EventArgs e)
{
string str = @"Data Source=J:\BerendinaSW\Payment20150408\Payment2015\Social.sdf;Persist Security Info=True";
SqlConnection con = new SqlConnection(str);
try{
con.Open();
}
catch(Exception ){
MessageBox.Show("cannot connect");
}
SqlCommand cm = new SqlCommand("SELECT * FROM [admin]",con);
cm.Connection = con;
SqlDataReader reader = null;
reader = cm.ExecuteReader(); // error showing in here it is mentioning connection closed
while (reader.Read())
{
if (textBox1.Text ==(reader["username"].ToString()) && textBox2.Text == (reader["password"].ToString()))
{
label8.Visible = true;
}
else
{
label9.Visible = true;
textBox1.Text = "";
textBox2.Text = "";
}
}
}
推荐答案
这是一个SDF文件,而不是SQL Server数据库。
使用SQLCE类而不是SQL服务器:
SqlCeConnection [ ^ ]
SqlCeCommand [ ^ ]
等等。
That's an SDF file, not a SQL Server database.
Use SQLCE classes instead of SQL server:
SqlCeConnection[^]
SqlCeCommand[^]
And so forth.
请使用此..
Please use this ..
private void button5_Click(object sender, EventArgs e)
{
string str = @"Data Source=J:\BerendinaSW\Payment20150408\Payment2015\Social.sdf;Persist Security Info=True";
SqlConnection con = new SqlConnection(str);
try{
con.Open();
}
catch(Exception ){
MessageBox.Show("cannot connect");
}
SqlCommand cm = new SqlCommand("SELECT * FROM [admin]",con);
SqlDataReader reader = null;
con.Open();
reader = cm.ExecuteReader();
con.Close();
while (reader.Read())
{
if (textBox1.Text ==(reader["username"].ToString()) && textBox2.Text == (reader["password"].ToString()))
{
label8.Visible = true;
}
else
{
label9.Visible = true;
textBox1.Text = "";
textBox2.Text = "";
}
}
}
谢谢
AARIF SHAIKH
Thanks
AARIF SHAIKH
private void button5_Click(object sender,EventArgs e)
{
string str = @数据源= J:\ BeendinaSW \ Payment20150408 \Payment2015 \Social.sdf; Persist Security Info = True;
SqlConnection con = new SqlConnection(str );
尝试{
if(con.State == ConnectionState.Open)
con.Close ();
con.Open();
}
catch(例外){
MessageBox.Show(无法连接);
}
SqlCommand cm = new SqlCommand(SELECT * FROM [admin],con);
cm.Connection = con;
SqlDataReader reader = null;
reader = cm.ExecuteReader(); //错误显示在这里它提到连接关闭
而(reader.Read())
{
if(textBox1.Text = =(reader [username]。ToString())&& textBox2.Text ==(reader [password]。ToString()))
{
label8 .Visible = true;
}
else
{
label9.Visible = true;
textBox1.Text =;
textBox2.Text =;
}
}
}
private void button5_Click(object sender, EventArgs e)
{
string str = @"Data Source=J:\BerendinaSW\Payment20150408\Payment2015\Social.sdf;Persist Security Info=True";
SqlConnection con = new SqlConnection(str);
try{
if(con.State==ConnectionState.Open)
con.Close();
con.Open();
}
catch(Exception ){
MessageBox.Show("cannot connect");
}
SqlCommand cm = new SqlCommand("SELECT * FROM [admin]",con);
cm.Connection = con;
SqlDataReader reader = null;
reader = cm.ExecuteReader(); // error showing in here it is mentioning connection closed
while (reader.Read())
{
if (textBox1.Text ==(reader["username"].ToString()) && textBox2.Text == (reader["password"].ToString()))
{
label8.Visible = true;
}
else
{
label9.Visible = true;
textBox1.Text = "";
textBox2.Text = "";
}
}
}
这篇关于c急剧的数据库连接问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文