繁体   English   中英

'。'附近的语法不正确

[英]Incorrect syntax near '.'

我正在尝试将数据库表与数据网格绑定,但是我在代码sda.fill(dt);第35行中遇到了问题sda.fill(dt); 但我在第35行遇到一个例外

'。'附近的语法不正确。

码:

namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            SqlConnection cn = new SqlConnection(@"Data Source=192.168.0.61\newsql;Initial Catalog=AIETraining;User ID=AIETrainingAccount;Password=Training@1234");

            cn.Open();
            Response.Write("Connection established");

            SqlCommand command = new SqlCommand("Select * from Gursimran 
            Singh.publishers", cn);

            DataTable dt = new DataTable();
            dt.Columns.AddRange(new DataColumn[5] { 
                       new DataColumn("pub_id", typeof(int)),
                       new DataColumn("pub_name",typeof(string)),
                       new DataColumn("city",typeof(string)),
                       new DataColumn("state",typeof(string)),
                       new DataColumn("Country",typeof(string))});

            SqlDataAdapter sda = new SqlDataAdapter();

            using (command)
            {
                using (sda) 
                {
                     command.Connection = cn;
                     sda.SelectCommand = command;

                     using(dt)
                     {
                          sda.Fill(dt);

                          GridView1.DataSource = dt;
                          GridView1.DataBind();
                     }
                }
            }

            cn.Close();
        }
    }
}

在下面尝试

处理SQL关键字/空格

namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            SqlConnection cn = new SqlConnection(@"Data Source=192.168.0.61\newsql;Initial Catalog=AIETraining;User ID=AIETrainingAccount;Password=Training@1234");

            cn.Open();
            Response.Write("Connection established");

            SqlCommand command = new SqlCommand("Select * from [Gursimran Singh].[publishers]", cn);

            DataTable dt = new DataTable();
            dt.Columns.AddRange(new DataColumn[5] { 
                       new DataColumn("pub_id", typeof(int)),
                       new DataColumn("pub_name",typeof(string)),
                       new DataColumn("city",typeof(string)),
                       new DataColumn("state",typeof(string)),
                       new DataColumn("Country",typeof(string))});

            SqlDataAdapter sda = new SqlDataAdapter();

            using (command)
            {
                using (sda) 
                {
                     command.Connection = cn;
                     sda.SelectCommand = command;

                     using(dt)
                     {
                          sda.Fill(dt);

                          GridView1.DataSource = dt;
                          GridView1.DataBind();
                     }
                }
            }

            cn.Close();
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM