将数据插入数据库的问题 [英] Problem to insert data into database

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

问题描述

我将数据插入数据库,面临某些问题。我的代码看起来像这样;

在第36行显示错误.cmd.ExecuteNonQuery();



I am inserting the data to database, facing certain problem. My Codes Looks like this;
showing error on Line No 36. cmd.ExecuteNonQuery();

<pre>Server Error in '/' Application.

Must declare the scalar variable "@txtBox1".

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Data.SqlClient.SqlException: Must declare the scalar variable "@txtBox1".

Source Error: 


Line 34: 
Line 35:         con.Open();
Line 36:         cmd.ExecuteNonQuery();
Line 37:         con.Close();
Line 38: 

Source File: c:\Users\Atta\Documents\Visual Studio 2012\WebSites\WebSite3\Page2.aspx.cs    Line: 36 

Stack Trace: 





我尝试了什么:





What I have tried:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.Security;
using System.Configuration;

public partial class Page2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connect"].ToString());
        
        
        string query = "insert into Register(FName,LName,age,TelNo,Address,gender) values (@txtBox1.text, @txtBox2.text, @txtBox3.text, @txtBox4.text, @txtBox5.text, @txtBox6.text)";

        SqlCommand cmd = new SqlCommand(query, con);
        cmd.Parameters.AddWithValue("@FName", txtBox1 .Text );
        cmd.Parameters.AddWithValue("@LName", TxtBox2.Text );
        cmd.Parameters.AddWithValue("@age", TxtBox3 .Text );
        cmd.Parameters.AddWithValue("@TelNo", TxtBox4 .Text );
        cmd.Parameters.AddWithValue("@Address", TxtBox5 .Text );
        cmd.Parameters.AddWithValue("@gender", TxtBox6 .Text );
        //add the rest

        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();

    }
}

推荐答案

错误是说参数名称不匹配。声明SQL时,使用@ txtBox1.Text作为占位符参数,但是当您添加参数时,使用@FName。更改INSERT语句以使用与Parameters.AddWithValue()一起添加的相同参数名称。
The error is saying that the parameter names do not match up. When you declare your SQL you use @txtBox1.Text as a placeholder parameter but then when you add parameters you use @FName. Change your INSERT statement to use the same parameters names that you are adding with Parameters.AddWithValue().


在您的代码中,您需要在下面更改。

字符串查询=插入注册表(FName,LName,年龄,TelNo,地址,性别)值(@ txtBox1.text,@ txtBox2.text,@ txtBox3.text,@ txtBox4.text,@ txtBox5.text,@ txtBox6.text) ;



应改为使用如下参数



string query =insert into Register( FName,LName,年龄,TelNo,地址,性别)值(@ FName,@ LName,@ age,@ TelNo,@ Address,@ gender);



我还建议将其他文件中的代码(最好是其他逻辑层,例如数据访问层)分开,以实现松耦合。
In your code you need below change.
string query = "insert into Register(FName,LName,age,TelNo,Address,gender) values (@txtBox1.text, @txtBox2.text, @txtBox3.text, @txtBox4.text, @txtBox5.text, @txtBox6.text)";

should be changed to use parameters like below

string query = "insert into Register(FName,LName,age,TelNo,Address,gender) values (@FName, @LName, @age, @TelNo, @Address, @gender)";

I would also recommend to separate the code in other file (preferably to other logical layer e.g. Data Access Layer) to achieve loose coupling.


你的代码中有一个错误,一个错误line ....

You have an error in your code fro one, one this line....
cmd.Parameters.AddWithValue("@FName", txtBox1 .Text );





在术语txtBox1 chan之后你不能有空格ge到txtBox1.Text



You cannot have a space after the term txtBox1 change to txtBox1.Text


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

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