绑定sql数据集使用 [英] binding sql dataset using

查看:110
本文介绍了绑定sql数据集使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

先生!

我已经为sql数据集的插入查询编写了代码。

我想在datagridview中显示相同的数据。请考虑以下代码:



sir!
I have written the code for the insert query for the sql dataset.
and i want to show the same data in datagridview. pls consider this code:

private void btnSave_Click(object sender, EventArgs e)
      {
          string s = dtpdue_date.Value.ToString();
          string s1 = dtpcompletiondate.Value.ToString();

          string connectionstr = "Data Source=TUSHAR-PC;Initial Catalog=callcenter;Persist Security Info=True;User ID=sa;Password=password@123";
          string sql = "Insert into tasks(t_id,Name,Priority,status,Due_date,description,date of completion)"+txtT_id.Text+",'"+txtname.Text+"','"+cbopriority.Text+",'"+cboStatus.Text+"','"+ s+"','"+txtcomments.Text+"'.'"+s1+"')";
          da = new SqlDataAdapter(sql,connectionstr);
          ds = new DataSet();
          connectionstr.Close();
              txtT_id.Text = " ";
              txtname.Text = " ";
              cbopriority.Text = " ";
              cboStatus.Text = " ";
              txtcomments.Text = " ";
              dtpdue_date.Text = " ";
              dtpcompletiondate.Text = "";


          }
      }

推荐答案

更新你的代码这个



Update your code like this

private void btnSave_Click(object sender, EventArgs e)
{
  string s = dtpdue_date.Value.ToString();
  string s1 = dtpcompletiondate.Value.ToString();
           
  string connectionstr = "Data Source=TUSHAR-PC;Initial Catalog=callcenter;Persist Security Info=True;User ID=sa;Password=password@123";
  string sql = "Insert into tasks(t_id,Name,Priority,status,Due_date,description,date of completion) values('"+txtT_id.Text+"','"+txtname.Text+"','"+cbopriority.Text+",'"+cboStatus.Text+"','"+ s+"','"+txtcomments.Text+"'.'"+s1+"')";
    
 DataSet dataSet = new DataSet();
 using (SqlConnection connection =
        new SqlConnection(connectionstr))
    {
        SqlDataAdapter adapter = new SqlDataAdapter();
        adapter.SelectCommand = new SqlCommand(
            sql, connection);
        adapter.Fill(dataset);
        dataGridView1.DataSource = dataset;
    }
}

确保连接字符串的顺序正确。

make sure that your connection string is in proper order.


这篇关于绑定sql数据集使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆