数据未插入数据库 [英] Data not inserting into database

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

问题描述

我遇到的问题是当我点击保存按钮时,我的数据没有被提交到数据库或数据表中。



什么应该发生的是该实例的用户(或我)应该在两个文本框中输入信息,然后单击保存按钮以在数据库中输入信息。但是,它不会保存在数据库中,也不会显示在gridview中,即使它显示并且看起来正在保存数据。



我的代码中没有错误。这是一个简单的签名形式,我正在创建,所以我不担心此时的SQL注入。虽然我会在完成项目后添加代码以防止sql注入。我将继续调试并寻找解决方案。如果有人可以回答或解决这个问题,将不胜感激。



我的代码如下,请注意我没有从我自己编写的互联网上复制并粘贴此代码。再次感谢你的时间,如果有人可以帮助我。







 SqlConnection con =  new  SqlConnection( @ 数据源=(LocalDB)\ MSSQLLocalDB; AttachDbFilename = | DataDirectory | \ ComputerLab.mdf; Integrated Security = True); 
SqlCommand cmd = new SqlCommand( sp_insert ,con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue( @ LASTNAME,metroTextBox1.Text);
cmd.Parameters.AddWithValue( @ COMPUTER,metroTextBox2.Text);
con.Open();
int i = cmd.ExecuteNonQuery();

con.Close();

if (i!= 0
{
MessageBox.Show(i + 数据保存);
}





我的尝试:



我调试了程序。测试了连接字符串。到目前为止我没有想到的任何事情似乎都有助于这种情况。

解决方案

首先要检查的是,每次启动程序时都不会覆盖数据库 - 数据目录是项目的子文件夹吗?您是否查询发布版本或调试版本?



使用连接详细信息连接到该数据库后,尝试在Sql Server Management Studio中运行以下代码您的代码。

 插入 进入 ComputerLab (LASTNAME,COMPUTER)'  LASTNAME''  COMPUTER'

如果报告任何错误,请解决错误 - 您可能在该表上有一列不能为NULL,但您没有插入例如。



尝试直接从SSMS运行存储过程

 sp_insert '  LASTNAME''  COMPUTERNAME' 

和看看是否显示任何错误。



您的数据库中是否有多个模式?尝试

  SELECT  *  FROM  dbo.ComputerLab 


The Problem I have is that my data is not being submitted into the database or the datatable at all when I click on the save button.

What supposed to happen is that the user (or me) for this instance is supposed to enter the information in two textboxes then click on the save button to enter the information in the database. However it doesn't save in the database or does show in the gridview even though it says and looks like the data is being saved.

There are no errors in my code. It's a simple sign in form that i'm creating so i'm not to worried about sql injection at this moment. although I will add code to prevent sql injection once i'm done with the project. I will continue to debug and look for solutions. If anyone can answer or address this issue it would be greatly appreciated.

My code is below and please note I did not copy and paste this code from the internet I wrote it myself. Again thank you for your time if anyone can help me.



SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\ComputerLab.mdf;Integrated Security=True");
            SqlCommand cmd = new SqlCommand("sp_insert", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@LASTNAME", metroTextBox1.Text);
            cmd.Parameters.AddWithValue("@COMPUTER", metroTextBox2.Text);
            con.Open();
            int i = cmd.ExecuteNonQuery();

            con.Close();

            if (i != 0)
            {
                MessageBox.Show(i + "Data Saved");
            }



What I have tried:

I have debugged the program. Tested the connection string. nothing I have thought of so far has seemed to help the situation any.

解决方案

First thing to check is that you are not overwriting the database everytime you start your program - is the datadirectory a sub-folder of your project? Are you querying the release version or the debug version if it is?

Try running the following code in Sql Server Management Studio after connecting to that database using the connection details from your code.

insert into ComputerLab (LASTNAME,COMPUTER) values('LASTNAME','COMPUTER')

If any errors are reported then address those errors - you may have a column on that table that cannot be NULL but which you are not inserting for example.

Try running the stored procedure directly from SSMS

sp_insert 'LASTNAME', 'COMPUTERNAME'

and see if any errors are shown.

Do you have multiple schemas within your database? Try

SELECT * FROM dbo.ComputerLab


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

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