C# SQL数据库 查询结果为空
SqlConnectioncnn=newSqlConnection("DataSource=pc-liujunyi;InitialCatalog=DBTrains;Per...
SqlConnection cnn = new SqlConnection("Data Source=pc-liujunyi;Initial Catalog=DBTrains;Persist Security Info=True;User ID=sa;Password=18");
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "select s1.TrainID,s1.StationName,s1.StopTime,s2.StationName,s2.StopTime,s2.Distance-s1.Distance from Runlist s1,Runlist s2 where s1.StationName like '徐州' and s2.StationName like '北京' and s1.TrainID=s2.TrainID";
cmd.Connection = cnn;
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = cmd;
cnn.Open();
DataSet ds = new DataSet();
sda.Fill(ds, "Runlist");
cnn.Close();
GridView3.DataSource = ds;
GridView3.DataBind();
调试时候ds为空?哪里错了
在数据库中查询能够得到结果,在程序里面就看不到了!表字段如下:
TrainID varchar(10) Unchecked
StationID smallint Unchecked
StationName varchar(40) Unchecked
Arrivesort smallint Unchecked
Arrivedate varchar(10) Unchecked
StartTime varchar(6) Unchecked
StopTime varchar(6) Unchecked
Distance smallint Unchecked
PriceHard smallint Unchecked
PriceHard2 smallint Unchecked
PriceSoft smallint Unchecked
PriceSoftberth smallint Unchecked
Unchecked 展开
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "select s1.TrainID,s1.StationName,s1.StopTime,s2.StationName,s2.StopTime,s2.Distance-s1.Distance from Runlist s1,Runlist s2 where s1.StationName like '徐州' and s2.StationName like '北京' and s1.TrainID=s2.TrainID";
cmd.Connection = cnn;
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = cmd;
cnn.Open();
DataSet ds = new DataSet();
sda.Fill(ds, "Runlist");
cnn.Close();
GridView3.DataSource = ds;
GridView3.DataBind();
调试时候ds为空?哪里错了
在数据库中查询能够得到结果,在程序里面就看不到了!表字段如下:
TrainID varchar(10) Unchecked
StationID smallint Unchecked
StationName varchar(40) Unchecked
Arrivesort smallint Unchecked
Arrivedate varchar(10) Unchecked
StartTime varchar(6) Unchecked
StopTime varchar(6) Unchecked
Distance smallint Unchecked
PriceHard smallint Unchecked
PriceHard2 smallint Unchecked
PriceSoft smallint Unchecked
PriceSoftberth smallint Unchecked
Unchecked 展开
4个回答
展开全部
sql语句错了 like 后面缺少'%'
应该是select s1.TrainID,s1.StationName,s1.StopTime,s2.StationName,s2.StopTime,s2.Distance-s1.Distance from Runlist s1,Runlist s2 where s1.StationName like '%徐州%' and s2.StationName like '%北京%' and s1.TrainID=s2.TrainID
应该是select s1.TrainID,s1.StationName,s1.StopTime,s2.StationName,s2.StopTime,s2.Distance-s1.Distance from Runlist s1,Runlist s2 where s1.StationName like '%徐州%' and s2.StationName like '%北京%' and s1.TrainID=s2.TrainID
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你那个sql语句有问题 而且1楼 2楼的语句效率不高
建议这样改,使用内连接查询。
select s1.TrainID,s1.StationName,s1.StopTime,s2.StationName,s2.StopTime,s2.Distance-s1.Distance from Runlist s1 inner join Runlist s2 on s1.TrainID=s2.TrainID where s1.StationName like '%徐州%' and s2.StationName like '%北京%'
建议这样改,使用内连接查询。
select s1.TrainID,s1.StationName,s1.StopTime,s2.StationName,s2.StopTime,s2.Distance-s1.Distance from Runlist s1 inner join Runlist s2 on s1.TrainID=s2.TrainID where s1.StationName like '%徐州%' and s2.StationName like '%北京%'
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
SqlConnection cnn = new SqlConnection("Data Source=pcliujunyi;Initial Catalog=DBTrains;Persist Security Info=True;User ID=sa;Password=18");
SqlDataAdapter sda = new SqlDataAdapter("select s1.TrainID,s1.StationName,s1.StopTime,s2.StationName,s2.StopTime,s2.Distance-s1.Distance from Runlist s1,Runlist s2 where s1.StationName like '%徐州%' and s2.StationName like '%北京%' and s1.TrainID=s2.TrainID", cnn);
DataSet Ds = new DataSet();
Da.Fill(Ds, "Runlist");
GridView3.DataSource = ds;
GridView3.DataBind();
SqlDataAdapter sda = new SqlDataAdapter("select s1.TrainID,s1.StationName,s1.StopTime,s2.StationName,s2.StopTime,s2.Distance-s1.Distance from Runlist s1,Runlist s2 where s1.StationName like '%徐州%' and s2.StationName like '%北京%' and s1.TrainID=s2.TrainID", cnn);
DataSet Ds = new DataSet();
Da.Fill(Ds, "Runlist");
GridView3.DataSource = ds;
GridView3.DataBind();
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你把语句拿到SQL里去运行,就知道错在哪了。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |