如何在SQL数据库中插入数据 [英] How to insert data in SQL database

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

问题描述


我正在编写以下代码以在SQL数据库代码中插入数据,但可以正常工作,但数据库中没有数据.请为此提供帮助.我会很感激的.我的代码如下:

Hi,
I''m writing following code to insert data in SQL database code is working properly but no data is going in the database.Please help me with this. I''ll really appreciate. My code is as follows:

conn.Close();
conn.Open();
string insertstring = "INSERT INTO dbo.login([username],[password])VALUES(@username,@password)";
SqlCommand cmd1 = new SqlCommand(insertstring, conn);
cmd1.Parameters.AddWithValue("@username", username.Text);
cmd1.Parameters.AddWithValue("@password", password.Text);
cmd1.ExecuteNonQuery();

conn.Close();

Response.Redirect("confirm.aspx");




这是注册页面的代码,在confirm.aspx页面上,我具有继续按钮,再次使我们进入登录页面.当我创建一个新帐户时,它接受新用户,但是我的数据库中没有数据




It is a code for signup page And on confirm.aspx page I''ve Continue button which again takes us to login page. When I create a new account its accepting the new user but there is no data in my database

推荐答案

很显然,这在专业水平上是不可接受的代码,但是如果您只是在玩耍,这是一个很好的起点.我相信executenonquery返回一个数字来告诉您查询更改了多少行.抓住它,看看它是否为0.在重定向之前放置一个断点,并在该点检查数据库.如果有数据,请尝试找出为什么重置数据库的原因.
并且,阅读一些有关如何正确构造数据库驱动项目的信息.看起来确实有人会使用代码,我希望没有人为此付出代价,但是您也可以学习使自己的代码也可维护
Obviously this is unacceptable code on a professional level, but if you''re just playing around, it''s a fine starting place. I believe executenonquery returns a number to tell you how many rows were changed by the query. Grab it and see if it''s 0. Put a breakpoint before the redirect, and check your database at that point. If the data is there, then try to work out why your database gets reset.

And, do some reading up on how to properly structure a database driven project. This does look like code someone will use, I hope no-one is paying for it, but you may as well learn to make your own code maintainable also


注意Christian告诉您的内容.您的代码仅能很好地发挥作用,而不是生产质量的代码.

除此之外,我看不到设置您的连接字符串.通常,在创建SqlConnection对象时将其作为参数传递.另外,如果在该代码块周围进行try/catch操作,则可能会发现问题所在.
Pay attention to what Christian told you. Your code is only good enough for playing around, and is not production quality code.

Beyond that, I don''t see your connection string being set. You normally pass it as a parameter when you create your SqlConnection object. Also, if you put a try/catch around that block of code, you''ll probably be able to tell what''s wrong.


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

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