数据不在表中插入没有显示 [英] Data not in insert in table no eeror display

查看:69
本文介绍了数据不在表中插入没有显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

if (txtpassword.Text == txtconform.Text)
                {
                    
                    con.Open();
                    string str1 = "insert into ram(fn,ln,un,pw,con,bod,q1,ans1,q2,ans2) values('" + txtfirstname.Text + "','" + txtlastname.Text + "','" + txtusername.Text + "','" + txtpassword.Text + "','" + txtcontake.Text + "','" + txtdateTimePicker.Value+ "','" + cmbq1.Text + "','" + txtans1.Text + "','" + cmbq2.Text + "','" + txtans2.Text + "')";
                    SqlCommand cmd = new SqlCommand(str1, con);
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("Data Save");
                    con.Close();
                    reset();
                }
                else
                {
                    MessageBox.Show("Plase reenter your conform password");
                    txtconform.Text = "";
                    txtconform.Focus();
                }





我的尝试:



查询是执行但是数据没有插入表中如果程序运行然后输入数据工作但turminet程序比不插入或不工作此数据



What I have tried:

query is execute but data not insert in table if program ranning then inputed data work but turminet program than it not insert or not work this data

推荐答案

您的查询容易受到SQL注入攻击。你应该使用参数化查询来建立你的用户输入的sql查询,而不是连接的字符串。



许多学生(特别是)反驳这个评论但我是唯一的一个使用数据库。但是,使用参数化查询通常可以克服其他问题,例如不匹配的单引号,日期格式等。



将您的查询更改为类似的内容

Your query is vulnerable to SQL Injection attacks. You should use parameterized queries to build your sql query from user input, not concatenated strings.

Many students (in particular) counter that comment with "but I am the only one using the database". However using parameterized queries can often overcome other issues such as mismatched single quotes, date formats etc.

Change your query to something like this
var con = new SqlConnection();
con.Open();
string str1 = "insert into ram(fn,ln,un,pw,con,bod,q1,ans1,q2,ans2) values(@firstname,@lastname,@username,@password,@contake,@dateTime,@cmbq1,@ans1,@cmbq2,@ans2)";
SqlCommand cmd = new SqlCommand(str1, con);
cmd.Parameters.AddWithValue("@firstname", txtfirstname.Text);
cmd.Parameters.AddWithValue("@lastname", txtlastname.Text);
cmd.Parameters.AddWithValue("@username", txtusername.Text);
cmd.Parameters.AddWithValue("@password", txtpassword.Text);
cmd.Parameters.AddWithValue("@contake", txtcontake.Text);
cmd.Parameters.AddWithValue("@datetime", txtdateTimePicker.Value);
cmd.Parameters.AddWithValue("@cmbq1", cmbq1.Text);
cmd.Parameters.AddWithValue("ans1", txtans1.Text);
cmd.Parameters.AddWithValue("@cmbq2", cmbq2.Text);
cmd.Parameters.AddWithValue("ans2", txtans2.Text);



我同意@ FES-Sitecore,如果你没有收到报告的错误,那么你可能有一个try-catch块忽略错误。但是,很可能是导致错误的日期(txtdateTimePicker.Value)。使用上面建议的参数也可以解决这个问题,它取决于列的类型 bod


这篇关于数据不在表中插入没有显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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