c# - 您在 mysql 中使用 c# 指定了无效的列序数

标签 c# mysql

我尝试使用此代码在某些标签中使用 C# 显示 MySQL 数据库中的数据,但它一直告诉我我指定了无效的列序号

 private void store_Load(object sender, EventArgs e)
                {
                    string constring = "datasource=localhost;port=3306;username=root";
                    string Query = "select Value from birth.store;";
                    MySqlConnection c = new MySqlConnection(constring);
                    MySqlCommand cmd = new MySqlCommand(Query, c);
                    MySqlDataReader reader;
                    try
                    {
                        c.Open();
                        reader = cmd.ExecuteReader();
                        while (reader.Read())
                        {

                            label3.Text = reader.GetInt32(1).ToString();
                            label4.Text = reader.GetInt64(0).ToString();
                            label6.Text = reader.GetInt64(8).ToString();
                            label10.Text = reader.GetInt64(5).ToString();
                            label14.Text = reader.GetInt64(7).ToString();
                            label13.Text = reader.GetInt64(13).ToString();
                            label11.Text = reader.GetInt64(10).ToString();
                            label20.Text = reader.GetInt64(12).ToString();
                            label19.Text = reader.GetInt64(16).ToString();
                            label17.Text = reader.GetInt64(14).ToString();
                            label26.Text = reader.GetInt64(15).ToString();
                            label32.Text = reader.GetInt64(4).ToString();
                            label31.Text = reader.GetInt64(6).ToString();
                            label23.Text = reader.GetInt64(3).ToString();
                            label27.Text = reader.GetInt64(2).ToString();
                            label25.Text = reader.GetInt64(11).ToString();
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }

最佳答案

您向 GetInt32() 传递的数字对于查询中的列数而言太大。您传递给 reader.GetInt32()reader.GetInt64() 的索引是查询中 SELECT 语句中该列的位置,从 0 开始。查询如下所示:

string Query = "select Value from birth.store;";

只有一列(SELECTFROM 之间的名称),因此任何大于 0 的数字如果传递给 reader.GetInt32() 都是无效的。查询中的列越多,可以传递的数字就越大。

1 列示例:

string Query = "select Value from birth.store;";
// database connection and reader execution go here
string Value = reader.GetInt32(0).toString(); // This gets the column Value

3 列示例:

// We're selecting 3 columns now
// 0 = Id
// 1 = Value
// 2 = Name
string Query = "select Id, Value, Name from birth.store;";
// database connection and reader execution go here

string Value = reader.GetInt32(1).toString(); // This gets the column Value

// This gets the column Id. Even though it's not the first one we pulled from
// the reader, it's still #0 on the list of 3 columns.
string Id = reader.GetInt32(0).toString(); 
string Name = reader.GetInt32(2).toString(); // This gets the column Name

// This will throw an exception. You're trying to get the 4th item on a list
// that starts with 0, but there are only 3 items (numbered 0, 1, 2).
string AnotherValue = reader.GetInt32(3).toString();

关于c# - 您在 mysql 中使用 c# 指定了无效的列序数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30135140/

相关文章:

C# 数据库中的多个并行插入

c# - 如何以编程方式检查电脑上是否存在 MS Excel?

C#静态类为什么要用?

mysql - 从每日最低值中选择数据

java - 在不启动事务的情况下通过 Hibernate 对 MySQL 数据库运行查询的含义是什么?

C#计算打开文件的MD5?

mysql - select * into outfile 即使对于 root 也不起作用

php - 不显示 PHP 文件中的数据

php - Google Chart 在 SQL 字符串之间添加每一天

c# - 在派生自 Control 的 ASP.NET 自定义控件中呈现自关闭标记