不要以窗口形式插入值 [英] dono insert value in window form

查看:55
本文介绍了不要以窗口形式插入值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的,我无法插入表格中。这里我使用windowform和sql数据库。



i使用以下代码:



添加用户这是连接字符串:

Dear i m unable to insert in table . here i m using windowform and sql data base .

i use following code :

add user this is connection string :

SqlConnection con = new SqlConnection("Data Source=RIZM9\\SQLEXPRESS; Initial Catalog=DynamicDB; integrated security= true; Timeout=45;");

string qry = "INSERT INTO User (Name,Mobile,EmailId,Gender) VALUES( '" + textBox1.Text + "' , '" + textBox2.Text + "','" + textBox3.Text + "' , '" + genderText + "')";
           
SqlCommand cmd = new SqlCommand(qry, con);
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Inserted Successfully");
this.Hide();





但值不是插入表中。 plz帮我如何以窗口形式插入数据。



But Value not INserted in table . plz help me how to insert data in window form .

推荐答案

试试这个

try this
string qry = "INSERT INTO  [dbo].[user] (Name,Mobile,EmailId,Gender) VALUES( '" + textBox1.Text + "' , '" + textBox2.Text + "','" + textBox3.Text + "' , '" + genderText + "')";





注意:了解 SQL注入 [ ^ ]



根据以下评论更新: < br $> b $ b





Note: be aware of SQL Injection[^]

updated based on thebelow comments:


string qry = "INSERT INTO [dbo].[User] (Name,Mobile,EmailId,Gender) VALUES( @Name,@Mobile,@EmailId,@Gender)";

           SqlCommand cmd = new SqlCommand(qry, con);
           cmd.Parameters.AddWithValue("@Name", textBox1.Text);
           cmd.Parameters.AddWithValue("@Mobile", textBox2.Text);
           cmd.Parameters.AddWithValue("@EmailId", textBox3.Text);
           cmd.Parameters.AddWithValue("@Gender", genderText);
           cmd.ExecuteNonQuery();


这篇关于不要以窗口形式插入值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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