Mysql 查询 datagridview C#
当我从 ASP C# 调用该查询时,该查询不会返回值,但是当我连接 MySQL 服务器并键入相同的查询时,它会返回正确的值。我无法发现问题所在。
这是代码段:
try
{
personquery = "select b.* from booking b, makes m "+
"where m.personid="+
DataDeneme1.login.personid.ToString() +
"and m.bookingno=b.bookingno";
con = new MySqlConnection(System.Configuration.ConfigurationManager.AppSettings.Get("connectionString"));
cmd.CommandText = personquery;
con.Open();
cmd.Connection = con;
adap = new MySqlDataAdapter(personquery, con);
adap.Fill(ds);
// CheckBoxList1.DataSource = ds;
// CheckBoxList1.DataBind();
GridView1.DataSource = ds;
GridView1.DataBind();
con.Close();
}
catch (Exception ex)
{
Response.Write(ex.Message);
Response.Write(ex.StackTrace);
}
mysql服务器的输入和输出:
mysql> select b.* from booking b, makes m where m.personid=1 and m.bookingno=b.bookingno;
+-----------+-----------------+--------------+-------------+------------+-------------+
| bookingno | reservationdate | dropoffplace | pickupplace | pickupdate | dropoffdate |
+-----------+-----------------+--------------+-------------+------------+-------------+
| 8 | 2011-05-09 | Ankara | Ankara | 2011-05-10 | 2011-05-15 |
| 9 | 2011-05-09 | Ankara | Ankara | 2011-05-20 | 2011-05-25 |
+-----------+-----------------+--------------+-------------+------------+-------------+
2 rows in set (0.00 sec)
以及异常消息....
您的 SQL 语法有错误; 检查对应的手册 您的 MySQL 服务器版本 附近使用的正确语法 'm.bookingno=b.bookingno' 在第 1 行 MySql.Data.MySqlClient.MySqlStream.ReadPacket() 在 MySql.Data.MySqlClient.NativeDriver.GetResult(Int32& 受影响的行,Int32&插入ID)在 MySql.Data.MySqlClient.Driver.GetResult(Int32 statementsId, Int32&受影响的行, Int32&插入ID)在 MySql.Data.MySqlClient.Driver.NextResult(Int32 语句 ID) 位于 MySql.Data.MySqlClient.MySqlDataReader.NextResult() 在 MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior 行为)在 MySql.Data.MySqlClient.MySqlCommand.ExecuteDbDataReader(CommandBehavior 行为)在 System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior 行为)在 System.Data.Common.DbDataAdapter.FillInternal(数据集 数据集、DataTable[] 数据表、Int32 startRecord,Int32 maxRecords,字符串 srcTable、IDbCommand 命令、 CommandBehavior 行为) at System.Data.Common.DbDataAdapter.Fill(数据集 数据集,Int32 开始记录,Int32 maxRecords,字符串 srcTable, IDbCommand 命令、CommandBehavior 行为)在 System.Data.Common.DbDataAdapter.Fill(数据集 数据集)位于 DataDeneme1.customerview.loadList() 中 E:\VisualStudioProjects\DataDeneme1\DataDeneme1\customerview.aspx.cs:行 38
The query does not return value when I call it from ASP C# however when I connect the MySQL server and type the same query it returns right values. I couldn't discover the problem.
Here is the code segment:
try
{
personquery = "select b.* from booking b, makes m "+
"where m.personid="+
DataDeneme1.login.personid.ToString() +
"and m.bookingno=b.bookingno";
con = new MySqlConnection(System.Configuration.ConfigurationManager.AppSettings.Get("connectionString"));
cmd.CommandText = personquery;
con.Open();
cmd.Connection = con;
adap = new MySqlDataAdapter(personquery, con);
adap.Fill(ds);
// CheckBoxList1.DataSource = ds;
// CheckBoxList1.DataBind();
GridView1.DataSource = ds;
GridView1.DataBind();
con.Close();
}
catch (Exception ex)
{
Response.Write(ex.Message);
Response.Write(ex.StackTrace);
}
The input and the output from mysql server:
mysql> select b.* from booking b, makes m where m.personid=1 and m.bookingno=b.bookingno;
+-----------+-----------------+--------------+-------------+------------+-------------+
| bookingno | reservationdate | dropoffplace | pickupplace | pickupdate | dropoffdate |
+-----------+-----------------+--------------+-------------+------------+-------------+
| 8 | 2011-05-09 | Ankara | Ankara | 2011-05-10 | 2011-05-15 |
| 9 | 2011-05-09 | Ankara | Ankara | 2011-05-20 | 2011-05-25 |
+-----------+-----------------+--------------+-------------+------------+-------------+
2 rows in set (0.00 sec)
and Exception message....
You have an error in your SQL syntax;
check the manual that corresponds to
your MySQL server version for the
right syntax to use near
'm.bookingno=b.bookingno' at line 1 at
MySql.Data.MySqlClient.MySqlStream.ReadPacket()
at
MySql.Data.MySqlClient.NativeDriver.GetResult(Int32&
affectedRow, Int32& insertedId) at
MySql.Data.MySqlClient.Driver.GetResult(Int32
statementId, Int32& affectedRows,
Int32& insertedId) at
MySql.Data.MySqlClient.Driver.NextResult(Int32
statementId) at
MySql.Data.MySqlClient.MySqlDataReader.NextResult()
at
MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior
behavior) at
MySql.Data.MySqlClient.MySqlCommand.ExecuteDbDataReader(CommandBehavior
behavior) at
System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior
behavior) at
System.Data.Common.DbDataAdapter.FillInternal(DataSet
dataset, DataTable[] datatables, Int32
startRecord, Int32 maxRecords, String
srcTable, IDbCommand command,
CommandBehavior behavior) at
System.Data.Common.DbDataAdapter.Fill(DataSet
dataSet, Int32 startRecord, Int32
maxRecords, String srcTable,
IDbCommand command, CommandBehavior
behavior) at
System.Data.Common.DbDataAdapter.Fill(DataSet
dataSet) at
DataDeneme1.customerview.loadList() in
E:\VisualStudioProjects\DataDeneme1\DataDeneme1\customerview.aspx.cs:line
38
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我敢打赌这与动态值
DataDeneme1.login.personid.ToString()
有关,因为异常指出了下一段文本。确保您的值符合您的预期,空白/空白值会导致此错误。更新
根据您的评论,我看到了我认为存在的问题。如果值为
1
那么结果:将是:
没有空间,因此将其添加到初始查询创建中:
这将导致:
I would bet it has to do with the dynamic value
DataDeneme1.login.personid.ToString()
, since the exception states the next piece of text. Ensure your value is what you are expecting, a blank/whitespace value would cause this error.UPDATE
Based on you comment I saw what I believe to be the issue. If the value is
1
then the result of this:Would be:
No space, so add that to the initial query creation:
Which would result in:
确保“and m.bookingno=b.bookingno”中有一个空格。我的意思是在“and”之前,就像“and”一样。
Make sure there is a space in " and m.bookingno=b.bookingno". I mean before "and", like " and".