SqlCommand。准備方法要求所有可變長度參數具有顯式設置的非零大小

[英]SqlCommand.Prepare method requires all variable length parameters to have an explicitly set non-zero Size


I tried to insert some values in SQL database by the code:

我試圖通過代碼在SQL數據庫中插入一些值:

var connstring = ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString; ;
var conn = new SqlConnection(connstring);
conn.Open();
SqlCommand command = new SqlCommand("INSERT INTO [Trainer] (Name, ID, [Trainee Counter], image, [Mobile NO], Email, Password) VALUES(:v1, :v2,:v3,:v4,:v5,:v6,:v7);", conn);

command.Parameters.Add(new SqlParameter("v1", SqlDbType.VarChar));
command.Parameters.Add(new SqlParameter("v2", SqlDbType.Int));
command.Parameters.Add(new SqlParameter("v3", SqlDbType.Int));
command.Parameters.Add(new SqlParameter("v4", SqlDbType.VarBinary));
command.Parameters.Add(new SqlParameter("v5", SqlDbType.VarChar));
command.Parameters.Add(new SqlParameter("v6", SqlDbType.VarChar));
command.Parameters.Add(new SqlParameter("v7", SqlDbType.VarChar));
command.Prepare();
command.Parameters[0].Value = TextBox1.Text;
command.Parameters[1].Value = 0;
command.Parameters[2].Value = 0;
command.Parameters[3].Value = FileUpload1.FileBytes;
command.Parameters[4].Value = TextBox3.Text;
command.Parameters[5].Value = TextBox4.Text;
command.Parameters[6].Value = TextBox5.Text;


command.ExecuteNonQuery();

conn.Close();

But I always get this exception:

但我總是會有這樣的例外:

SqlCommand.Prepare method requires all variable length parameters to have an explicitly set non-zero Size.

SqlCommand。准備方法要求所有可變長度參數具有顯式設置的非零大小。

Actually I have ported this code from one of my C++/CLI project with PostgreSQL, and it works fine on that project.

實際上,我已經用PostgreSQL從我的一個c++ /CLI項目中移植了這段代碼,它在那個項目中工作得很好。

4 个解决方案

#1


1  

You can should add your parameter like this instead:

可以這樣添加參數:

command.Parameters.Add(@"v1", SqlDbType.VarChar).Value = TextBox1.Text;
command.Parameters.Add(@"v2", SqlDbType.Int).Value = 0;
command.Parameters.Add(@"v3", SqlDbType.Int).Value = 0;
command.Parameters.Add(@"v4", SqlDbType.VarBinary).Value = FileUpload1.FileBytes;
command.Parameters.Add(@"v5", SqlDbType.VarChar).Value = TextBox3.Text;
command.Parameters.Add(@"v6", SqlDbType.VarChar).Value = TextBox4.Text;
command.Parameters.Add(@"v7", SqlDbType.VarChar).Value = TextBox5.Text;


command.ExecuteNonQuery();

conn.Close();

But personnaly I prefer to add my parameter like this:

但我個人更喜歡添加如下參數:

cmd.Parameters.AddWithValue(@"v1",  TextBox1.Text);
cmd.Parameters.AddWithValue(@"v2", 0);
cmd.Parameters.AddWithValue(@"v3", 0);
cmd.Parameters.AddWithValue(@"v4", FileUpload1.FileBytes);
cmd.Parameters.AddWithValue(@"v5", TextBox3.Text);
cmd.Parameters.AddWithValue(@"v6", TextBox4.Text);
cmd.Parameters.AddWithValue(@"v7", TextBox5.Text);

#2


2  

you should set max size of variable length parameters

您應該設置可變長度參數的最大大小

replace all varchar (and varbinary) parameters:

替換所有varchar(和varbinary)參數:

command.Parameters.Add(new SqlParameter("v1", SqlDbType.VarChar));

with:

:

command.Parameters.Add(new SqlParameter("v1", SqlDbType.VarChar, 50));

#3


1  

Setting the parameter.Size to the database's maximum size of the column corrected the problem for me when I ran into this.

設置參數。數據庫的最大列大小的大小糾正了我遇到這個問題的原因。

For Each parameter As SqlClient.SqlParameter In command.Parameters
   parameter.Value = row.Item(parameter.SourceColumn)
   parameter.Size = Me.mData.Columns.Item(parameter.SourceColumn).MaxLength
Next

#4


0  

SqlCommand.Prepare() requires all variable parameters tobe non zero .

please check wether any value is empty.

請檢查是否有空值。

Check for Null or Empty:

檢查空或空:

command.Parameters[0].Value = (!String.IsNullOrEmpty(TextBox1.Text.ToString()))?TextBox1.Text.ToString():"Empty";
command.Parameters[1].Value = 0;
command.Parameters[2].Value = 0;
command.Parameters[3].Value = FileUpload1.FileBytes;
command.Parameters[4].Value =  (!String.IsNullOrEmpty(TextBox3.Text.ToString()))?TextBox3.Text.ToString():"Empty";
command.Parameters[5].Value = (!String.IsNullOrEmpty(TextBox4.Text.ToString()))?TextBox4.Text.ToString():"Empty";
command.Parameters[6].Value =  (!String.IsNullOrEmpty(TextBox5.Text.ToString()))?TextBox5.Text.ToString():"Empty";
关注微信公众号

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2013/11/15/7def55cb1c11425b344f74a2395248f.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com