不支持关键字: “string connstring”
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Data;
using System.Data.SqlClient;
namespace 作业
{
public class OperataDB
{
/// <summary>
/// 连接数据库
/// </summary>
public static string ConnString
{
get
{
using (StreamReader dr = File.OpenText(@"F:\作业\作业\作业\connDB.txt"))
{
return dr.ReadLine();
}
}
}
public static void PrepareCommand(SqlCommand cmd, SqlConnection conn, string cmdText)
{
if (conn.State != ConnectionState.Open)
{
conn.Open();
}
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = cmdText;
}
/// <summary>
/// 执行数据读取操作
/// </summary>
/// <param name="cmdtext">SQl语句</param>
/// <returns>如果获取到值返回TRUE</returns>
public static bool ExecuteReader(string cmdText)
{
using (SqlConnection conn = new SqlConnection(ConnString)) //---此处提示 不支持关键字: “string connstring”。(哪错误,谢了)
{
SqlCommand cmd = new SqlCommand();
PrepareCommand(cmd,conn,cmdText);
SqlDataReader dr = cmd.ExecuteReader();
return dr.Read();
}
}
}
}
另附
private void btnLogin_Click(object sender, EventArgs e)
{
string sql = "select * from UserInfo where UserName='" + txtUsrename.Text.Trim().ToLower() + "'and UserPwd='" + txtPassword.Text.Trim() + "'";
if (OperataDB.ExecuteReader(sql))
{
// username = dr["UserName"].ToString();
frmMain frm = new frmMain();
frm.Show();
this.Hide();
}
else
{
MessageBox.Show("用户名或密码错误", "出错了", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
------解决方案--------------------OperataDB.ConnString
------解决方案--------------------没有问题;
return dr.ReadLine();你看看这里返回的是什么。
------解决方案--------------------"F:\作业\作业\作业\connDB.txt"
connDB.txt中是什么内容?
路径名用英文试试。
------解决方案--------------------看看ConnString什么内容
------解决方案--------------------连接字符串错了
------解决方案--------------------看看ConnString 提示详细信息