Mysql 查询 datagridview C#

发布于 2024-11-05 21:15:28 字数 2759 浏览 3 评论 0原文

当我从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笑梦风尘 2024-11-12 21:15:28

我敢打赌这与动态值 DataDeneme1.login.personid.ToString() 有关,因为异常指出了下一段文本。确保您的值符合您的预期,空白/空白值会导致此错误。

更新
根据您的评论,我看到了我认为存在的问题。如果值为 1 那么结果:

personquery = "select b.* from booking b, makes m  "+
              "where m.personid="+ 
              DataDeneme1.login.personid.ToString() +
              "and m.bookingno=b.bookingno";

将是:

select b.* from booking b, makes m
where m.personid=1and m.bookingno=b.bookingno

没有空间,因此将其添加到初始查询创建中:

personquery = "select b.* from booking b, makes m  "+
              "where m.personid= "+ 
              DataDeneme1.login.personid.ToString() +
              " and m.bookingno=b.bookingno";

这将导致:

select b.* from booking b, makes m
where m.personid= 1 and m.bookingno=b.bookingno

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:

personquery = "select b.* from booking b, makes m  "+
              "where m.personid="+ 
              DataDeneme1.login.personid.ToString() +
              "and m.bookingno=b.bookingno";

Would be:

select b.* from booking b, makes m
where m.personid=1and m.bookingno=b.bookingno

No space, so add that to the initial query creation:

personquery = "select b.* from booking b, makes m  "+
              "where m.personid= "+ 
              DataDeneme1.login.personid.ToString() +
              " and m.bookingno=b.bookingno";

Which would result in:

select b.* from booking b, makes m
where m.personid= 1 and m.bookingno=b.bookingno
花开雨落又逢春i 2024-11-12 21:15:28

确保“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".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文