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

我在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是“无法识别的转义序列”。

杨魅力
浏览 247回答 3
3回答

江户川乱折腾

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

aluckdog

定义一个类,写数据库连接,执行语句,读取等方法,然后在要用的类里实例化一下,在调用就可以了,我用的是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的函数修改下

红颜莎娜

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验证
打开App,查看更多内容
随时随地看视频慕课网APP