参数化查询'(@nick varchar(25),@ pass varchar(500),@ date datetime)插入到'中需要未提供的参数'@pass'. [英] The parameterized query '(@nick varchar(25),@pass varchar(500),@date datetime)insert into' expects the parameter '@pass', which was not supplied.

查看:47
本文介绍了参数化查询'(@nick varchar(25),@ pass varchar(500),@ date datetime)插入到'中需要未提供的参数'@pass'.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在将批量数据插入到表中.它将引发此错误:将参数化查询(@nick varchar(25),@ pass varchar(500),@ date datetime)插入"预期为参数"@pass"(未提供)

参数@pass在SQL语句中,我将其添加到Comand参数中,DataTable在捷克语密码中有一个名为"Heslo"的列,并且在其中插入了"1234",所以我想知道为什么会引发此错误!有人帮忙吗?

客户代码:

 公共  class 客户端
    {
        私有 字符串 ConnectionString = " 数据源= PETA3NEC_NTBK;初始目录=沙滩排球;集成安全性= True" ;
        公共 SqlConnection连接{ get ; ;}
        公共 SqlCommand命令{ get ; 设置; }
        公共 SqlDataAdapter适配器{ get ; 设置; }
        公共 SqlDataReader Reader { get ; 设置; }
        ///  <  > 
 /// 构造函数
 ///  <  /summary  > 
 公共 Client()
        {
            Connection =  SqlConnection(ConnectionString);
        }
        公共 布尔 Connect()
        {
            尝试
            {
                Connection.Open();
                返回 ;
            }
            捕获(异常)
            {
                
                投掷;
            }
        }
        公共 布尔 Disconnect()
        {
            尝试
            {
                Connection.Close();
                返回 ;
            }
            捕获(异常)
            {

                投掷;
            }
        }
    } 


插入代码:

 公共  bool  groupInsert( int 计数, int  maxid, int  maxid2)
        {
            客户client =  Client();
            尝试
            {
                client.Connect();
                client.Command = client.Connection.CreateCommand();
                client.Command.CommandText = "  ;
                client.Command.Parameters.Add(" ,System.Data.SqlDbType.VarChar, 25 " );
                client.Command.Parameters.Add(" ,System.Data.SqlDbType.VarChar, 500 " );
                client.Command.Parameters.Add(" ,System.Data.SqlDbType.DateTime, 10 " );

                // 准备数据集
                DataSet ds =  DataSet();
                DataTable table =  DataTable(" );
                table.Columns.Add("  typeof (字符串));
                table.Columns.Add("  typeof (字符串));
                table.Columns.Add("  typeof (DateTime));

                随机兰特=  Random();
                 for ( int  i =  0 ; i < 计数; i ++)
                {
                    DataRow行= table.NewRow();
                    row [ 0 ] = "  + i;
                    row [ 1 ] = "  ;
                    row [ 2 ] = DateTime.Now;
                    table.Rows.Add(row);
                }
                ds.Tables.Add(table);
                //  
                client.Adapter =  System.Data.SqlClient.SqlDataAdapter();
                client.Adapter.InsertCommand = client.Command;
                client.Adapter.TableMappings.Add("  " );
                client.Adapter.UpdateBatchSize =  100 ;
                client.Command.UpdatedRowSource = UpdateRowSource.None;
                client.Adapter.Update(ds," );
                client.Disconnect();
                返回 ;
            }
            捕获(异常)
            {
                投掷;
            }
        } 



感谢您的答复!

-Pepin zHané

解决方案

尝试:Command.Parameters.AddWithValue()重载.目前.
尝试:Command.Parameters.AddWithValue()重载.

参考: MSDN:SqlParameterCollection.AddWithValue方法 [ ^ ]


Hi,
I am inserting in bulk datas to a table. It throws this error: The parameterized query ''(@nick varchar(25),@pass varchar(500),@date datetime)insert into'' expects the parameter ''@pass'', which was not supplied.

parameter @pass is in SQL statement, I am adding it into Comand Parameters, the DataTable has a column named "Heslo" it is in czech Password and I am inserting there "1234", so I wonder why is this error thrown!? Does anybody help?

Client code:

public class Client
    {
        private String ConnectionString = "Data Source=PETA3NEC_NTBK;Initial Catalog=Beach_Volleyball;Integrated Security=True";
        public SqlConnection Connection {get; set;}
        public SqlCommand Command { get; set; }
        public SqlDataAdapter Adapter { get; set; }
        public SqlDataReader Reader { get; set; }
        /// <summary>
        /// Constructor
        /// </summary>
        public Client()
        {
            Connection = new SqlConnection(ConnectionString);
        }
        public bool Connect()
        {
            try
            {
                Connection.Open();
                return true;
            }
            catch (Exception)
            {
                
                throw;
            }
        }
        public bool Disconnect()
        {
            try
            {
                Connection.Close();
                return true;
            }
            catch (Exception)
            {

                throw;
            }
        }
    }


insert code:

public bool groupInsert(int count, int maxid, int maxid2)
        {
            Client client = new Client();
            try
            {
                client.Connect();
                client.Command = client.Connection.CreateCommand();
                client.Command.CommandText = "insert into Player_Password values (@nick, @pass, @date)";
                client.Command.Parameters.Add("@nick", System.Data.SqlDbType.VarChar, 25, "Nickname");
                client.Command.Parameters.Add("@pass", System.Data.SqlDbType.VarChar, 500, "Heslo");
                client.Command.Parameters.Add("@date", System.Data.SqlDbType.DateTime, 10, "Datum_Aktualizace");

                //Prepare DataSet
                DataSet ds = new DataSet();
                DataTable table = new DataTable("DB_Player_Password");
                table.Columns.Add("Nickname", typeof(string));
                table.Columns.Add("Password", typeof(string));
                table.Columns.Add("Datum_Aktualizace", typeof(DateTime));

                Random rand = new Random();
                for (int i = 0; i < count; i++)
                {
                    DataRow row = table.NewRow();
                    row[0] = "PLA"+i;
                    row[1] = "1234";
                    row[2] = DateTime.Now;
                    table.Rows.Add(row);
                }
                ds.Tables.Add(table);
                //
                client.Adapter = new System.Data.SqlClient.SqlDataAdapter();
                client.Adapter.InsertCommand = client.Command;
                client.Adapter.TableMappings.Add("Player_Password", "DB_Player_Password");
                client.Adapter.UpdateBatchSize = 100;
                client.Command.UpdatedRowSource = UpdateRowSource.None;
                client.Adapter.Update(ds, "DB_Player_Password");
                client.Disconnect();
                return true;
            }
            catch (Exception)
            {
                throw;
            }
        }



Thanks for replies!

-Pepin z Hané

解决方案

Try: Command.Parameters.AddWithValue() overload.


Looks like parameters are added but values are not getting passed currently.
Try: Command.Parameters.AddWithValue() overload.

Refer: MSDN: SqlParameterCollection.AddWithValue Method [^]


这篇关于参数化查询'(@nick varchar(25),@ pass varchar(500),@ date datetime)插入到'中需要未提供的参数'@pass'.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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