我想从数据库中提取数据。我只需要显示前三列(0,1,2),而不是全部。我该怎么办?

>     string selectQuery = "select command line goes here where.... offerMadeBy='"+cbox1.Text.ToString()+"';";
>                 MySqlConnection sqlCOnnect = new MySqlConnection(RootDBConnection.myConnection);
>                 MySqlCommand sqlCmd = new MySqlCommand(selectQuery,sqlCOnnect);
>                 MySqlDataAdapter sqlAdapter;
>                 try {
>                     sqlAdapter = new MySqlDataAdapter();
>                     sqlAdapter.SelectCommand = sqlCmd;
>
>                     DataTable dbset = new DataTable();
>                     sqlAdapter.Fill(dbset);
>                     BindingSource bindSource = new BindingSource();
>
>                     _dataGridView.DataSource = null;
>                     bindSource.DataSource = dbset;
>                     _dataGridView.DataSource = bindSource;
>                     sqlAdapter.Update(dbset);
>
>
>                     if (sqlCOnnect.State == ConnectionState.Open) {
>                         sqlCOnnect.Close();
>                     }
>                 }catch(MySqlException ex){
>                     MessageBox.Show("Can't load data from DB.\nReason:"+ex.Message);
>                 }

最佳答案

您可以只从数据库中选择所需的字段。此外,您必须打开与SQL的连接并最后使用参数,例如:

string selectQuery = "select field1,field2,field3 from mytable where offerMadeBy=@param";
MySqlConnection sqlCOnnect = new MySqlConnection(RootDBConnection.myConnection);
MySqlCommand sqlCmd = new MySqlCommand(selectQuery,sqlCOnnect);
sqlCmd.Parameters.AddWithValue("@param", cbox1.Text.ToString());
try {
       sqlCOnnect.Open()
       using (sqlCOnnect)
       {
        ....
       }
      }

关于c# - 将值从MySQL DB加载到datagrid View 中的特定列,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28479553/

10-17 01:54