C# 您使用c在mysql中指定了无效的列序号
我尝试过用C在一些标签中显示MySQL数据库中的数据,但它总是告诉我指定了无效的列序号C# 您使用c在mysql中指定了无效的列序号,c#,mysql,C#,Mysql,我尝试过用C在一些标签中显示MySQL数据库中的数据,但它总是告诉我指定了无效的列序号 private void store_Load(object sender, EventArgs e) { string constring = "datasource=localhost;port=3306;username=root"; string Query = "select Value
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;";
SELECT和FROM之间只有一列名称,因此如果将大于0的任何数字传递给reader.GetInt32,则该数字无效。查询中的列越多,可以传递的数字就越高
具有1列的示例:
具有3列的示例:
@那我该怎么办do@GrantWinney我想在每个标签中显示一列的特定单元格的值,我尝试了这个code@GrantWinney你有没有其他代码来完成这个任务
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
// 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();