本文介绍了参数化查询'(@ name nvarchar(4000))插入dab(名称)值(@name)'需要参数'@name',这是未提供的。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友我想将表格数据插入数据库



这里是代码



Student.cs文件



Friends i want insert form data into database

here is code

Student.cs file

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

namespace Practice1
{


    public class Student
    {
        public int id { get; set;}
        public string name { get; set;}
        public void add(Student std)
        {
            string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
            using (SqlConnection con = new SqlConnection(CS))
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("insert into dab (name) values (@name)", con);
                cmd.Parameters.AddWithValue ("@name", SqlDbType.VarChar).Value=name;
                cmd.ExecuteNonQuery();

            }


        }

    }
}


And My connection string in web.config file

 <connectionstrings>
    <add name="DBCS">
      connectionString ="Data Source=BHUSHAN-PC\SQLEXPRESS; Initial Catalog=Practice;  Integrated Security=SSPI"
       providerName="System.Data.SqlClient"/>

  </add></connectionstrings>


Friends in Student.cs file

name is textbox id name....
<table><tbody><tr><td class="style2">
                    First Name</td></tr></tbody></table>
                <td class="style6">
                    <asp:TextBox ID="name" runat="server" ontextchanged="TextBox1_TextChanged">
                </td>
                <table><tbody><tr><td>

the error is

The parameterized query '(@name nvarchar(4000))insert into dab (name) values (@name)' expects the parameter '@name', which was not supplied.

help me out..friends...</td></tr></tbody></table>

推荐答案

cmd.Parameters.Add("@name", SqlDbType.VarChar).Value=name;


public void add(Student std)
        {
            string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
            using (SqlConnection con = new SqlConnection(CS))
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("insert into dab (name) values (@name)", con);
                cmd.Parameters.AddWithValue("@name", name);
                cmd.CommandType = CommandType.Text;
                cmd.ExecuteNonQuery();
            }
        }


这篇关于参数化查询'(@ name nvarchar(4000))插入dab(名称)值(@name)'需要参数'@name',这是未提供的。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 05:22