数据插入无效! ! [英] Data insertion is not working ! !

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

问题描述

我尝试使用Webform向数据库输入一些详细信息,但未获取值数据库.
当我尝试插入数据时没有显示错误,它将重定向到InsertSuccess.aspx页.
但是当我检查数据库中输入的数据时,没有更多数据.
请告诉我更正的路径.
在此先感谢


这是我的代码

I tried to enter some details to my database using a webform and it did not get the values database.
When I tried to insert data it did not show me error, it will re-direct to the InsertSuccess.aspx page.
but when I check the entered data in database, no more data.
Please show me the path to correct it.
Thanks in advance


Here is my code

protected void Button1_Click(object sender, EventArgs e)
   {

       string a = TextBox1.Text;
       string b = TextBox2.Text;
       string c = TextBox3.Text;
       string d = TextBox4.Text;
       int ef = Convert.ToInt32(TextBox5.Text);
       int f = Convert.ToInt32(TextBox6.Text);
       int mo = Convert.ToInt32(TextBox12.Text);
       string g = TextBox7.Text;
       string h = TextBox11.Text;
       string i = DropDownList6.SelectedItem.ToString();
       string j = DropDownList7.SelectedItem.ToString();
       string k = DropDownList3.SelectedItem.ToString();
       string l = DropDownList4.SelectedItem.ToString();
       string m = DropDownList5.SelectedItem.ToString();
       string n = TextBox8.Text;
       string o = TextBox9.Text;
       OleDbConnection conn = new OleDbConnection();
       OleDbCommand comm = new OleDbCommand();
       conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=K:\Morn\db1.mdb";
       try
       {
           conn.Open();
           string myQuery = "INSERT INTO Lawyer values (''" + a + "'',''" + b + "'',''" + c + "'',''" + d + "''," + ef + "," + f + "," + mo + ",''" + g + "'',''" + h + "'',''" + i + "'',''" + j + "'',''" + k + "'',''" + l + "'',''" + m + "'',''" + n + "'',''" + o + "'')";
           comm = new OleDbCommand(myQuery, conn);
           comm.ExecuteNonQuery();

           Response.Redirect("InsertSuccess.aspx");


       }
       catch (Exception ex)
       {
           //Response.Redirect("DatabaseError.aspx");
           Response.Write(ex.Message.ToString());

       }
       finally
       {
           conn.Close();
       }

推荐答案

步骤1:
使用DEBUGGER,在执行前检查形成了什么"myQuery".

步骤2:
尝试将此查询直接形成到您的数据库中.请参阅,这是否正确形成并起作用.

步骤3:
现在,为什么要创建两次"comm"对象?完成后,您可以仅使用其属性来定义查询和连接字符串. comm.ConnectionString = "......";
之类的
步骤4:
''comm.ExecuteNonQuery();''设置一个整数返回值. ExecuteNonQuery将返回受影响的行数.如果得到的值大于零,请进行重定向.

希望以上所有这些步骤都能告诉您问题所在.试试.
Step 1:
Using DEBUGGER, check what ''myQuery'' is formed just before executing it.

Step 2:
Try this query formed directly into your DB. See, if this formed correctly and works.

Step 3:
Now, why have you created ''comm'' object two times? Once done, you can just use its properties to define query and connection string. Something like, comm.ConnectionString = "......";

Step 4:
Set a integer return value for ''comm.ExecuteNonQuery();''. ExecuteNonQuery woud return the number of rows affected. If you get that more than zero then do a redirect.

Hope all these steps above should tell you the issue. Try.


嘿,调试页面并检查变量"string myquery"的结果.

您可以直接在查询浏览器中运行结果,并查看是否出现错误.

在构建query时可能存在问题.请注意. :-)
hey debug your page and check result of your variable "string myquery".

you can run result directly in query browser and see to it are you getting ne error.

there might be problem in building query.please see to it . :-)



像这样使用参数
hi ther
use parameters instead like this
 string myQuery = "INSERT INTO Laywers(fields go here separated by commas) VALUES(?,?,?)"; // Add a question mark according to how many fields u have 6 fields then put 6 question mark
comm.CommandQuery = myQuery;
comm.Parameters.AddWithValue("?", Textbox1.Text); // link each parameter to corresponding field
comm.Parameter.AddWithValue("?", Textbox2.Text); // repeat  step for each parameter


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

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