每次点击插入按钮时,在数据库表中重复插入数据 [英] Inserting data repeatedly in database table when every am clicking on insert button

查看:54
本文介绍了每次点击插入按钮时,在数据库表中重复插入数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




每次点击插入按钮时,在数据库表中重复插入数据。



任何人都可以给我解决方案。



Hi
Inserting data repeatedly in database table when every am clicking insert button.

can anyone give me solution .

ServiceController[] services = ServiceController.GetServices();
{
              dataGridView1.DataSource = services ;
}
 

 
 string conn = "Data Source=HostName;Initial Catalog=MSNETDB;Integrated Security=True;Pooling=False";
            SqlConnection mycon = new SqlConnection(conn);
            mycon.Open();
                foreach (DataGridViewRow row in dataGridView1.Rows)
                {
                    
                    string Displayname = row.Cells[3].Value.ToString();
                    string ServiceName = row.Cells[4].Value.ToString();
                    string Status = row.Cells[5].Value.ToString();
                    string ServiceType = row.Cells[6].Value.ToString();              
                    
    
                    SqlCommand com = new SqlCommand(conn, mycon);
                   
                    com .CommandText = "insert into SystemServiceInfo(DisplayName,ServiceName,Status,ServiceType)values('" + Displayname + "','" + ServiceName + "','" + Status + "','" + ServiceType + "')";
                    
                   com.ExecuteNonQuery();
                    com.Parameters.Clear();
                    com.Dispose();
 

                }
            
                mycon.Close();
                mycon.Dispose();





谢谢&问候

sam.198979



Thanks & Regards
sam.198979

推荐答案

你好Sam,



你可能将需要编写一段代码来检查插入的值是否存在于数据库表中,即您的情况下是否存在SystemServiceInfo。



如果值存在,可以跳过插入查询和提示消息。你必须匹配整套值。



希望这会有所帮助。



Rgds

Himanshu
Hello Sam,

You probably will have to write a piece of code which checks if the values inserted don''t exist in the Database table i.e. SystemServiceInfo in your case.

If the values exist you can skip the Insert Query and prompt with a message.You will have to match the full set of values.

Hope this helps.

Rgds
Himanshu


这篇关于每次点击插入按钮时,在数据库表中重复插入数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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