数据未保存到数据库 [英] Data not saving to database

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

问题描述

我有一个包含大约20个文本框的表单。用户输入数据并将数据提交到数据库中。除了诸如名字,姓氏等用户信息的数据之外,所有数据都被保存。它没有保存到Table3中。为什么会这样?这是我的代码。



I have a form that has about 20 textboxes. The user enters the data and submits the data into the database. All of the data gets saved except the data of the user information like First name, Last Name and so on. It is not saving into Table3. Why is this happening? Here is my code.

SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["HotConnectionString"].ConnectionString);
        con.Open();
        SqlCommand cmd = new SqlCommand("Insert into Table1 (INST_ID, TOTAL_REVE, FINYR, DATE, INSTRUCTIO, RESEARCH, PUBLIC_SER, ACADEMIC_S, STUDENT_SE, INSTITUTIO, PHYSICAL_P, SCHOLARSHI, AUXILIARY_, HOSPITALS, INDEPENDEN, OTHEREXP, TOTASSETS, TOTLIABILITY, NoNEXPPERMRESASSETS, EXPENDABLE, UNRNETASSETS, TOTALREV, TUITFEES, CURRDEBT, LONGTERMDEBT, TOTALNETASSETS) values (@INST_ID, @TOTAL_REVE, @FINYR, @DATE, @INSTRUCTIO, @RESEARCH, @PUBLIC_SER, @ACADEMIC_S, @STUDENT_SE, @INSTITUTIO, @PHYSICAL_P, @SCHOLARSHI, @AUXILIARY_, @HOSPITALS, @INDEPENDEN, @OTHEREXP, @TOTASSETS, @TOTLIABILITY, @NoNEXPPERMRESASSETS, @EXPENDABLE, @UNRNETASSETS, @TOTALREV, @TUITFEES, @CURRDEBT, @LONGTERMDEBT, @TOTALNETASSETS); Insert into TableFIN2 (INST_ID, LongName, TOTAL_REVE, FINYR, DATE, INSTRUCTIO, RESEARCH, PUBLIC_SER, ACADEMIC_S, STUDENT_SE, INSTITUTIO, PHYSICAL_P, SCHOLARSHI, AUXILIARY_, HOSPITALS, INDEPENDEN, OTHEREXP, TOTASSETS, TOTLIABILITY, NoNEXPPERMRESASSETS, EXPENDABLE, UNRNETASSETS, TOTALREV, TUITFEES, CURRDEBT, LONGTERMDEBT, TOTALNETASSETS) values (@INST_ID, @LongName, @TOTAL_REVE, @FINYR, @DATE, @INSTRUCTIO, @RESEARCH, @PUBLIC_SER, @ACADEMIC_S, @STUDENT_SE, @INSTITUTIO, @PHYSICAL_P, @SCHOLARSHI, @AUXILIARY_, @HOSPITALS, @INDEPENDEN, @OTHEREXP, @TOTASSETS, @TOTLIABILITY, @NoNEXPPERMRESASSETS, @EXPENDABLE, @UNRNETASSETS, @TOTALREV, @TUITFEES, @CURRDEBT, @LONGTERMDEBT, @TOTALNETASSETS); Insert into Table3 (INST_ID, FirstName, MiddleName, LastName, Prefix, Suffix, Salutation, Title, Address1, Address2, City, State, Zip, Country, Phone, Fax, Email, DATE, accessLevel) values (@INST_ID, @FirstName, @MiddleName, @LastName, @Prefix, @Suffix, @Salutation, @Title, @Address1, @Address2, @City, @State, @Zip, @Country, @Phone, @Fax, @Email, @DATE, @accessLevel);", con);
        cmd.CommandType = CommandType.Text;

        cmd.Parameters.AddWithValue("@INST_ID", TextBoxINST_ID.Text);
        cmd.Parameters.AddWithValue("@TOTAL_REVE", TextBoxTROA.Text);
        cmd.Parameters.AddWithValue("@INSTRUCTIO", TextBoxInstr.Text);
        cmd.Parameters.AddWithValue("@RESEARCH", TextBoxRes.Text);
        cmd.Parameters.AddWithValue("@PUBLIC_SER", TextBoxPubS.Text);
        cmd.Parameters.AddWithValue("@ACADEMIC_S", TextBoxAcad.Text);
        cmd.Parameters.AddWithValue("@STUDENT_SE", TextBoxStudS.Text);
        cmd.Parameters.AddWithValue("@INSTITUTIO", TextBoxInstiS.Text);
        cmd.Parameters.AddWithValue("@PHYSICAL_P", TextBoxOperM.Text);
        cmd.Parameters.AddWithValue("@SCHOLARSHI", TextBoxSFEDA.Text);
        cmd.Parameters.AddWithValue("@AUXILIARY_", TextBoxAuxE.Text);
        cmd.Parameters.AddWithValue("@HOSPITALS", TextBoxHosS.Text);
        cmd.Parameters.AddWithValue("@INDEPENDEN", TextBoxIndeO.Text);
        cmd.Parameters.AddWithValue("@OTHEREXP", TextBoxOED.Text);
        cmd.Parameters.AddWithValue("@TOTASSETS", TextBoxTA.Text);
        cmd.Parameters.AddWithValue("@TOTLIABILITY", TextBoxTL.Text);
        cmd.Parameters.AddWithValue("@NoNEXPPERMRESASSETS", TextBoxNPRNA.Text);
        cmd.Parameters.AddWithValue("@EXPENDABLE", TextBoxETRNA.Text);
        cmd.Parameters.AddWithValue("@UNRNETASSETS", TextBoxTUNA.Text);
        cmd.Parameters.AddWithValue("@TOTALREV", TextBoxTR.Text);
        cmd.Parameters.AddWithValue("@TUITFEES", TextBoxTFN.Text);
        cmd.Parameters.AddWithValue("@CURRDEBT", TextBoxCD.Text);
        cmd.Parameters.AddWithValue("@LONGTERMDEBT", TextBoxLTD.Text);
        cmd.Parameters.AddWithValue("@DATE", TextBoxDATE.Text);
        cmd.Parameters.AddWithValue("@FINYR", TextBoxDATE2.Text);
        cmd.Parameters.AddWithValue("@TOTALNETASSETS", TextBoxTNA.Text);

        cmd.Parameters.AddWithValue("@FirstName", TextBoxFName.Text);
        cmd.Parameters.AddWithValue("@LastName", TextBoxLName.Text);
        cmd.Parameters.AddWithValue("@MiddleName", TextBoxMName.Text);
        cmd.Parameters.AddWithValue("@Prefix", TextBoxPrefix.Text);
        cmd.Parameters.AddWithValue("@Suffix", TextBoxSuffix.Text);
        cmd.Parameters.AddWithValue("@Salutation", TextBoxSal.Text);
        cmd.Parameters.AddWithValue("@Title", TextBoxTitle.Text);
        cmd.Parameters.AddWithValue("@Address1", TextBoxMA1.Text);
        cmd.Parameters.AddWithValue("@Address2", TextBoxMA2.Text);
        cmd.Parameters.AddWithValue("@City", TextBoxCity.Text);
        cmd.Parameters.AddWithValue("@State", TextBoxState.Text);
        cmd.Parameters.AddWithValue("@Zip", TextBoxZip.Text);
        cmd.Parameters.AddWithValue("@Country", TextBoxCoun.Text);
        cmd.Parameters.AddWithValue("@Phone", TextBoxTN.Text);
        cmd.Parameters.AddWithValue("@Fax", TextBoxFN.Text);
        cmd.Parameters.AddWithValue("@Email", TextBoxEA.Text);
        cmd.Parameters.AddWithValue("@accessLevel", TextBoxaccessLevel.Text);
        cmd.Parameters.AddWithValue("@LongName", lblSchool.Text);
        
        cmd.ExecuteNonQuery();
        con.Close();

推荐答案

有些事要尝试....



1.尝试使用 SQL或ODBC保留字 [ ^ ]带方括号,例如 [DATE] 。这可能不是问题,但是你应该避免使用某些单词作为列名,如果你觉得必须,请记住使用方括号技巧。



2.在
Some things to try ....

1. Try surrounding any columns that use SQL or ODBC Reserved words [^] with square brackets e.g. [DATE]. This probably isn't a problem here however you should avoid using certain words for column names, and if you feel you must, remember to use the square-bracket trick.

2. Put a break point on the line
cmd.Parameters.AddWithValue("@FirstName", TextBoxFName.Text);

行上设置一个断点并运行您的代码。

在断点处悬停在所有这些文本框值上并确保它们都包含一些文本。



如果有人不知道在其中包含任何文本,检查是否允许该数据库列上的空值。



3.如果他们都有文本,请确保它是适当的类型 - 例如确保你没有尝试用苹果更新int列



4.如果这不能解决问题,请尝试将更新更新到table3该多语句命令并单独运行它。目前尚不清楚您是否已经在使用Try-Catch块,但是您收到的任何错误消息都可能有助于确定问题。或者尝试直接对数据库运行它以查看返回的错误。

and run your code.
At the break point hover over all of those text box values and make sure that they all contain some text.

If some don't have any text in them, check to see if you have allowed nulls on that database column.

3. If they do all have text in them make sure it is the appropriate type - e.g. make sure you are not trying to update an int column with "apples"

4. If that doesn't sort your problem out, try taking the update to table3 out of that multi-statement command and run it by itself. It's not clear if you are already using a Try-Catch block but any error message you get back might help determine the problem. Or try running it directly against the database to see what errors are returned.


仅从那一点来说,它很难说,但我怀疑你在最后的INSERT中有一个SQL错误抛出异常,系统设置为忽略它。它可能像拼写错误的列名一样简单,也可能是一个包含太多信息的文本框。



所以做两件事:

1)使用 try ... catch 块为您提供异常详细信息,并记录或记录并报告问题 - 这样您就有机会找到答案它是什么。

2)在整个插入过程中使用一个事务,如果出现错误则取消它 - 这样你就不会在前两个表中找到孤立信息。 ..
From that alone, it's pretty difficult to tell, but I suspect that you have an SQL error in the final INSERT which is throwing an exception and your system is set to ignore it. It may be as simple as a misspelled column name, or a text box with too much information in it.

So do two things:
1) Use a try...catch block to give you the exception details, and either log or log and report the problem - that way you stand a chance of finding out what it is.
2) Use a transaction round the whole insert, and cancel it if you get an error - that way you don't end up with "orphaned" information in your first two tables...




你可以检查你的表table3传递的参数是@Email,@ DATE。

// ////////////////////////////////////////////////// ///////////

i认为这两个数据之后的数据没有插入

cmd.Parameters.AddWithValue(@DATE,TextBoxDATE.Text);

cmd.Parameters.AddWithValue(@ FINYR,TextBoxDATE2.Text);



DATE是sql中预定义的变量所以请把它改成一些OT她的名字,也检查了sql server中的DATE数据类型。

/////////////////////////////// ///////////////////////////////



由于这个原因,table3中的数据将为空。
Hi,
can you please check your table table3 passing parameters are like @Email, @DATE.
///////////////////////////////////////////////////////////////
i think the data after these two are not inserting
cmd.Parameters.AddWithValue(@DATE, TextBoxDATE.Text);
cmd.Parameters.AddWithValue(@FINYR, TextBoxDATE2.Text);

DATE is predefined variable in sql so please change it to some other name and also check DATE datatype in sql server.
//////////////////////////////////////////////////////////////

Due to this, data in your table3 will be empty.


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

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