这个c#里写的sql到底怎么了!

hahasong1213 2010-06-16 11:00:33
各位,不好意思,我刚学,下面是我想用datagripview这个控件,可是不知道为什么不显示数据,网上的内容又多又杂,越看越迷茫。。。。高手们,谁能教下我,难道是那个sql语句写错了??
--------------------------------------------------------------------------------------------------

private void btnSearch_Click_1(object sender, EventArgs e)
{
DataTable dt1 = new DataTable();
int i = 0; //intn=0;
string s1 = "", s2 = "";
string strsql;
s1 = textBox1.Text;
s2 = textBox3.Text;

i = Convert.ToInt32(textBox2.Text);
SqlConnection objSqlConnection = new SqlConnection("workstation id=Song;Integrated Security=SSPI;database=yohamilky");
objSqlConnection.Open();
MessageBox.Show("数据库连接成功", "好");
try
{
strsql = "select Order_ID,Milk_ID,Amount,Beginning_Day,Days from "
+ "Customer , Order where Customer.Customer_ID=Order.Customer_ID and Customer_name ="
+ s1 +" and building_NO ="
+ i + " and Door_NO = " + s2;
MessageBox.Show(strsql);
da1 = new SqlDataAdapter(strsql, objSqlConnection);
dt1.Clear();
dataGridView1.DataSource = dt1.DefaultView;
MessageBox.Show("添加成功!");
//SqlConnection con = new SqlConnection("context connection=true");
}
catch (Exception)
{
MessageBox.Show("意外!");
}
}
------------------------------------------------------------------------------


谢谢!
...全文
125 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
永生天地 2010-06-17
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 xys_777 的回复:]
Order 改为 [Order]
[/Quote]
表名用了sql 保留字需要加[]或""
永生天地 2010-06-17
  • 打赏
  • 举报
回复
Order 改为 [Order]
hahasong1213 2010-06-17
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 xys_777 的回复:]
这段对应该改,尤其是连接字符串时的 '
try
{
strsql = "select Order_ID,Milk_ID,Amount,Beginning_Day,Days from "
+ "Customer , Order where Customer.Customer_ID=Order.Customer_ID and Customer_name ='"
+ s1 +"……
[/Quote]
不好意思,忘改了! 非常感谢各位!!

---------------------
private void btnSearch_Click_1(object sender, EventArgs e)
{
DataTable dt1 = new DataTable();
int i = 0; //intn=0;
string s1 = "", s2 = "";
string strsql;
s1 = textBox1.Text;
s2 = textBox3.Text;

i = Convert.ToInt32(textBox2.Text);
SqlConnection objSqlConnection = new SqlConnection("server=Song;Integrated Security=SSPI;database=yohamilky");
objSqlConnection.Open();
MessageBox.Show("数据库连接成功", "好");
try
{
strsql = "select Order_ID,Milk_ID,Amount,Beginning_Day,Days from Customer,Order where Customer.Customer_ID = Order.Customer_ID and Customer_name ='"
+ s1 + "' and Building_NO =" + i + " and Door_NO = '" + s2 + "'";

MessageBox.Show(strsql);
da1 = new SqlDataAdapter(strsql, objSqlConnection);
da1.Fill(dt1);
dataGridView1.DataSource = dt1;
MessageBox.Show("添加成功!");
//SqlConnection con = new SqlConnection("context connection=true");
}
catch (Exception)
{
MessageBox.Show("意外!");
}
-----------------------------------
我写的是这样的,捕捉到的错误时 Order附近有语法错误!
hunterpo 2010-06-17
  • 打赏
  • 举报
回复
最好使用参数化查询避免 SQL Injection。参考:DataAdapter 参数 (ADO.NET)
needacoder 2010-06-17
  • 打赏
  • 举报
回复
我来学学
  • 打赏
  • 举报
回复
order 是关键字,应该使用[]括起来。

你调试程序的思路不对,你应该单步调试,设计sql语句的将其语句提取出来,在查询分析器里面运行,没有异常了,再继续调试程序。
hahasong1213 2010-06-17
  • 打赏
  • 举报
回复
哦,明白啦~谢谢各位!!!
永生天地 2010-06-16
  • 打赏
  • 举报
回复
这段对应该改,尤其是连接字符串时的 '
try
{
strsql = "select Order_ID,Milk_ID,Amount,Beginning_Day,Days from "
+ "Customer , Order where Customer.Customer_ID=Order.Customer_ID and Customer_name ='"
+ s1 +"' and building_NO ='"
+ i + "' and Door_NO = '" + s2+"'";
MessageBox.Show(strsql);
da1 = new SqlDataAdapter(strsql, objSqlConnection);
dt1.Clear();
da1.fill(dt1)
dataGridView1.DataSource = dt1;

MessageBox.Show("添加成功!");
//SqlConnection con = new SqlConnection("context connection=true");
}
catch (System.Data.SqlClient.SqlException e)
{
MessageBox.Show(e.Message);
}
catch (Exception)
{
MessageBox.Show("意外!");
}
}
wochuailimin 2010-06-16
  • 打赏
  • 举报
回复
SqlConnection objSqlConnection = new SqlConnection("workstation id=Song;Integrated Security=SSPI;database=yohamilky");
workstation你用的虚拟机吗?
strsql = "select Order_ID,Milk_ID,Amount,Beginning_Day,Days from "
+ "Customer , Order where Customer.Customer_ID=Order.Customer_ID and Customer_name ="
+ s1 +" and building_NO ="
+ i + " and Door_NO = " + s2;
是不是应该这样啊:
strsql="select Order_ID,Milk_ID,Amount,Beginning_Day,Days from Customer where Customer.Customer_ID=Order.Customer_ID and Customer_name ="
+ s1 +" and building_NO ="
+ i.tostring() + " and Door_NO = " + s2"
因为i是int,所以要tostring()一下
hahasong1213 2010-06-16
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 xys_777 的回复:]
da1 = new SqlDataAdapter(strsql, objSqlConnection);
dt1.Clear();
da1.fill(dt1)
dataGridView1.DataSource = dt1;
[/Quote]

加上了!
可是还是出意外~
永生天地 2010-06-16
  • 打赏
  • 举报
回复
da1 = new SqlDataAdapter(strsql, objSqlConnection);
dt1.Clear();
da1.fill(dt1)
dataGridView1.DataSource = dt1;

27,580

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧