求出各位大侠 坐等结果 c# 运行后出现 未处理 System.InvalidOperationException Message=实例失败。
代码SqlConnectionthisConnection=newSqlConnection(@"DataSource=20111009-2218\\SQLEXPRESS...
代码 SqlConnection thisConnection = new SqlConnection(
@"Data Source=20111009-2218\\SQLEXPRESS;" +
@"database=wujian.mdf;" +
@"Integrated SecuritY=True");
string sql = "select UserID,Password From ID_password where UserID =" + zhanghao.Text + ",Password=" + mima.Text + "";
SqlDataAdapter thisadapter = new SqlDataAdapter(sql, thisConnection);
//SqlCommandBuilder thiscommand = new SqlCommandBuilder(thisadapter);
DataSet thisDataset = new DataSet();
thisadapter.Fill(thisDataset,"ID_pssword");
if (thisDataset.Tables["ID_password"].Rows.Count == 0)
{
MessageBox.Show("用户名或密码错误!请重新登录!!!", "错误");
zhanghao.Clear();
mima.Clear();
zhanghao.Focus();
return;
}
else
{
Form2 xs = new Form2();
this.Hide();
xs.Show();
} 展开
@"Data Source=20111009-2218\\SQLEXPRESS;" +
@"database=wujian.mdf;" +
@"Integrated SecuritY=True");
string sql = "select UserID,Password From ID_password where UserID =" + zhanghao.Text + ",Password=" + mima.Text + "";
SqlDataAdapter thisadapter = new SqlDataAdapter(sql, thisConnection);
//SqlCommandBuilder thiscommand = new SqlCommandBuilder(thisadapter);
DataSet thisDataset = new DataSet();
thisadapter.Fill(thisDataset,"ID_pssword");
if (thisDataset.Tables["ID_password"].Rows.Count == 0)
{
MessageBox.Show("用户名或密码错误!请重新登录!!!", "错误");
zhanghao.Clear();
mima.Clear();
zhanghao.Focus();
return;
}
else
{
Form2 xs = new Form2();
this.Hide();
xs.Show();
} 展开
1个回答
展开全部
///数据库连接字符串没那么麻烦,一个字符串就搞定
string strConn = "Data Source=.;Initial CataLog=wujian;Integrated Security=True";
///使用using(…………) {} 可以象try{} catch(){} finally{} 那样避免错误
using ( SqlConnection thisConnection = new SqlConnection(strConn))
{
///主要错误出在这行,你的字符串拼接不对,少了单引号;
/// 字符串拼接拼接 推荐使用string.Format()
string sql = string.Format("select UserID,Password From ID_password where UserID ='{0}',Password='{1}' ",zhanghao.Text, mima.Text);
using ( SqlDataAdapter thisadapter = new SqlDataAdapter(sql, thisConnection))
{
DataSet thisDataset = new DataSet();
thisadapter.Fill(thisDataset, "ID_pssword");
if (thisDataset.Tables["ID_password"].Rows.Count == 0)
{
MessageBox.Show("用户名或密码错误!请重新登录!!!", "错误");
zhanghao.Clear();
mima.Clear();
zhanghao.Focus();
return;
}
else
{
Form2 xs = new Form2();
this.Hide();
xs.Show();
}
}
string strConn = "Data Source=.;Initial CataLog=wujian;Integrated Security=True";
///使用using(…………) {} 可以象try{} catch(){} finally{} 那样避免错误
using ( SqlConnection thisConnection = new SqlConnection(strConn))
{
///主要错误出在这行,你的字符串拼接不对,少了单引号;
/// 字符串拼接拼接 推荐使用string.Format()
string sql = string.Format("select UserID,Password From ID_password where UserID ='{0}',Password='{1}' ",zhanghao.Text, mima.Text);
using ( SqlDataAdapter thisadapter = new SqlDataAdapter(sql, thisConnection))
{
DataSet thisDataset = new DataSet();
thisadapter.Fill(thisDataset, "ID_pssword");
if (thisDataset.Tables["ID_password"].Rows.Count == 0)
{
MessageBox.Show("用户名或密码错误!请重新登录!!!", "错误");
zhanghao.Clear();
mima.Clear();
zhanghao.Focus();
return;
}
else
{
Form2 xs = new Form2();
this.Hide();
xs.Show();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询