连接数据库提示无法识别的转义序列

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yayun0516/article/details/40105927

当我们需要连接数据库进行数据操作的时候,有时会出现以下提示

代码区会有错误提示


问题代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace TreeViewDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //string connString = @"Data Source=localhost/SQL2005;Initial Catalog=renshi;Integrated Security=True";
            //SqlConnection con = new SqlConnection(connString);
            //con.Open();  
            SqlConnection con = new SqlConnection("server=localhost\SQL2005;uid=sa;pwd=1111qq;database=renshi");
            con.Open();
            SqlCommand com = new SqlCommand("select *from renshi",con);
            SqlDataReader dr = com.ExecuteReader();
            treeView1.Nodes.Clear();
            TreeNode tree = treeView1.Nodes.Add("课程名称");
            while(dr.Read())aa
            {
                TreeNode td = new TreeNode(dr[1].ToString());
                tree.Nodes.Add(td);
            }
            dr.Close();
            con.Close();
            treeView1.ExpandAll();
        }
    }
}

要解决此类问题只需将连接语句的server=localhost\SQL2005;改成server=localhost\\SQL2005;即可
展开阅读全文

没有更多推荐了,返回首页