无法将数据从datagridview插入数据库表 [英] Unable to insert data from datagridview into database table

查看:83
本文介绍了无法将数据从datagridview插入数据库表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





无法将数据从datagridview插入到数据库表中。



任何人都可以帮助解决这个问题。



Hi,

Unable to insert data from datagridview into database table .

can anyone help in solving this .

private void Form1_Load(object sender, EventArgs e)
 {
  ServiceController[] services = ServiceController.GetServices();
  dataGridView1.DataSource = services ;
 }


 public SqlConnection getsqlcon()
        {
            string conn = "Data Source=HostName;Initial Catalog=MSNETDB;Integrated Security=True;Pooling=False";
                SqlConnection mycon=new SqlConnection (conn);
                return mycon;
        }
        public bool getcomm(string connstring1)
        {
            SqlConnection sqlcon = this.getsqlcon();
            SqlCommand com = new SqlCommand(connstring1, sqlcon);
            sqlcon.Open();
            com.ExecuteNonQuery();
            com.Dispose();
            sqlcon.Close();
            sqlcon.Dispose();
            return true;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection();

            conn.ConnectionString = Helper.ConnectionString;

                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;
                try
                {
                string col1 = dataGridView1[3, dataGridView1.CurrentCell.RowIndex].Value.ToString();
                string col2 = dataGridView1[4, dataGridView1.CurrentCell.RowIndex].Value.ToString();
                string col3 = dataGridView1[5, dataGridView1.CurrentCell.RowIndex].Value.ToString();
                string col4 = dataGridView1[6, dataGridView1.CurrentCell.RowIndex].Value.ToString();
                string insert_sql="insert into Services(DisplayName,ServiceName,Status,ServiceType)values('"+col1 +"','"+col2 +"','"+col3 +"','"+col4+ "')";

                if (this.getcomm(insert_sql))
                {
                    MessageBox.Show("Insertion Success");
                }
                else
                {
                    MessageBox.Show("Insertion Failed");
                }
                }
                catch
                {
                }





感谢问候

sam.198979



Thanks Regards
sam.198979

推荐答案

删除try catch块。然后你会知道什么异常导致了这个问题,然后你可以解决它。让我知道它是否有效。
Remove try catch block. Then you will come to know what exception is causing this problem and then you can fix it. Let me know if it works or not.


这篇关于无法将数据从datagridview插入数据库表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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