【问题标题】:Mysql query datagridview c#mysql查询datagridview c#
【发布时间】:2011-08-22 01:14:47
【问题描述】:

当我从 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 statementId,Int32& 受影响的Rows, Int32& 插入 ID)在 MySql.Data.MySqlClient.Driver.NextResult(Int32 statementId) 在 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 行为)在 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:line 38

【问题讨论】:

  • 第 38 行是 --> adap.Fill(ds);
  • 他得到一个 SQL 错误,所以数据集填充甚至不会发生。

标签: c# mysql gridview dataadapter


【解决方案1】:

我敢打赌,它与动态值 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

【讨论】:

  • 我调试了代码,发现 personid 是“1”。没有问题
【解决方案2】:

确保 " 和 m.bookingno=b.bookingno" 中有空格。我的意思是在“and”之前,比如“and”。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-26
    • 1970-01-01
    • 2014-10-28
    相关资源
    最近更新 更多