为了账号安全,请及时绑定邮箱和手机立即绑定

提问c# 相关问题:连接SQL数据库代码出错!!?

提问c# 相关问题:连接SQL数据库代码出错!!?

C# C
杨魅力 2021-12-02 11:07:46
我在useing窗体的load事件写了      private void useing_Load(object sender, EventArgs e)        {            string strConnection = " Data Source=PC-20090625HIOS\SQLEXPRESS;Initial Catalog=student;Integrated Security=True";             SqlConnection objConnection = new SqlConnection(strConnection);            objConnection.Open();            string strcommand = "select * from record";            SqlCommand objcommand = new SqlCommand(strcommand, objConnection);           this.textBox1 .Text =strcommand ;           objConnection.Close();        }想要把record数据库里的数据调出来给textBox1但是好像不行!其中PC-20090625HIOS\SQLEXPRESS是我的SQL服务器名称,我是用windows验证的。但是编译器说PC-20090625HIOS\SQLEXPRESS是“无法识别的转义序列”。
查看完整描述

3 回答

?
江户川乱折腾

TA贡献1851条经验 获得超5个赞

PC-20090625HIOS\SQLEXPRESS 中包含一个转义字符"\",他会把之后的一个字符组合起来做转义:\S
你用两个斜杠试试.
PC-20090625HIOS\\SQLEXPRESS

查看完整回答
反对 回复 2021-12-06
?
aluckdog

TA贡献1847条经验 获得超7个赞

定义一个类,写数据库连接,执行语句,读取等方法,然后在要用的类里实例化一下,在调用就可以了,我用的是C#,你看看
class dosql
{
public OleDbConnection getcon()
{
string M_str_oledbcon = "连接字符串";
OleDbConnection myCon = new OleDbConnection(M_str_oledbcon);
return myCon;
}
public void getcom(string M_str_oledbstr)
{
OleDbConnection oledbcon = this.getcon();
oledbcon.Open();
OleDbCommand oledbcom = new OleDbCommand(M_str_oledbstr, oledbcon);
oledbcom.ExecuteNonQuery();
oledbcom.Dispose();
oledbcon.Close();
oledbcon.Dispose();
}
public DataSet getds(string M_str_oledbcstr, string M_str_table)
{
OleDbConnection oledbcon = this.getcon();
OleDbDataAdapter oledbda = new OleDbDataAdapter(M_str_oledbcstr, oledbcon);
DataSet myds = new DataSet();
oledbda.Fill(myds, M_str_table);
return myds;
}
public OleDbDataReader getread(string M_str_oledbstr)
{
OleDbConnection oledbcon = this.getcon();
OleDbCommand oledbcom = new OleDbCommand(M_str_oledbstr, oledbcon);
oledbcon.Open();
OleDbDataReader oledbread = oledbcom.ExecuteReader(CommandBehavior.CloseConnection);
return oledbread;

}
}
你对照VB的函数修改下


查看完整回答
反对 回复 2021-12-06
?
红颜莎娜

TA贡献1842条经验 获得超12个赞

string strConnection = " Data Source=PC-20090625HIOS\SQLEXPRESS;Initial Catalog=student;Integrated Security=True"; 改为string strConnection = " Data Source=PC-20090625HIOS\\SQLEXPRESS;Initial Catalog=student;Integrated Security=True"; 或者string strConnection =@ " Data Source=PC-20090625HIOS\SQLEXPRESS;Initial Catalog=student;Integrated Security=True"; 试试看,不行的话你就用用户加密码的别用windows验证

查看完整回答
反对 回复 2021-12-06
  • 3 回答
  • 0 关注
  • 246 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信