我正在尝试将数据库表与数据网格绑定,但是我在代码sda.fill(dt);
的第35行遇到问题,但是在第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();
}
}
}
答案 0 :(得分:2)
尝试以下
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();
}
}
}