异常:参数的长度超过了 C# 中 128 个字符的限制
Exception : The length of the parameter exceeds the limit of 128 characters in C#
我正在尝试使用 WCF 服务在 SQLServer 中插入值。
有 5 行要插入。
它给出了例外:
The length of the parameter exceeds the limit of 128 characters
是的,它的长度超过 128 characters.But 我已经声明了大小 NVarChar(4000)。
我搜索了这个站点和其他站点以了解并消除此异常
MedicineTestName = "1,crocin,2,aspirin,2,naproxen,3,ibuprofen,3,Ketaprofen";
代码:
public static string InsertHistory(string PatientId, string MedicineTestName, string CreateBy)
{
DataSet objdata;
object objSubjectReader = new object();
try
{
StringBuilder sb = new StringBuilder();
string[] wordswithcomma = MedicineTestName.Split(',');
for (int i = 0; i < wordswithcomma.Length -1 ; i=i+2)
{
string MedicineType = wordswithcomma[i];
string MedicineName = wordswithcomma[i + 1];
sb.Append("insert into tblMedicineHistory values(" + PatientId + "," + "'" + MedicineName + "'" + "," + MedicineType + ",GETDATE()," + "'" + CreateBy + "'" + ",GETDATE(),0);");
}
string sbo = sb.ToString();
SqlParameter[] param = new SqlParameter[1];
param[0] = new SqlParameter(sbo, System.Data.SqlDbType.NVarChar, 4000);
param[0].Value = sbo;
objdata = SqlHelper.ExecuteDataset(ConString, CommandType.StoredProcedure, "SP_MedicineHistory", param);
return JsonConvert.SerializeObject(objdata, Newtonsoft.Json.Formatting.Indented);
//return sbo;
}
catch (SqlException ex)
{
return objSubjectReader.ToString();
}
}
谢谢。
我觉得是在抱怨参数的name;您正在传递 sbo
作为参数名称,但 sbo
是 完全组成的 TSQL,这将...很长。注意:你的 SQL 是完全不安全的——也就是说 而不是 如何参数化 SQL!
单行的正确参数化 SQL 类似于:
const string sql = @"
insert into tblMedicineHistory (PatientID, MedicineName, MedicineType, ...)
values(@patientId, @medicineName, @medicineType, ...)";
var args = new[] {
new SqlParameter("@patientId", PatientId),
new SqlParameter("@medicineName", MedicineName),
new SqlParameter("@medicineType", MedicineType),
...
}
我正在尝试使用 WCF 服务在 SQLServer 中插入值。 有 5 行要插入。
它给出了例外:
The length of the parameter exceeds the limit of 128 characters
是的,它的长度超过 128 characters.But 我已经声明了大小 NVarChar(4000)。
我搜索了这个站点和其他站点以了解并消除此异常
MedicineTestName = "1,crocin,2,aspirin,2,naproxen,3,ibuprofen,3,Ketaprofen";
代码:
public static string InsertHistory(string PatientId, string MedicineTestName, string CreateBy)
{
DataSet objdata;
object objSubjectReader = new object();
try
{
StringBuilder sb = new StringBuilder();
string[] wordswithcomma = MedicineTestName.Split(',');
for (int i = 0; i < wordswithcomma.Length -1 ; i=i+2)
{
string MedicineType = wordswithcomma[i];
string MedicineName = wordswithcomma[i + 1];
sb.Append("insert into tblMedicineHistory values(" + PatientId + "," + "'" + MedicineName + "'" + "," + MedicineType + ",GETDATE()," + "'" + CreateBy + "'" + ",GETDATE(),0);");
}
string sbo = sb.ToString();
SqlParameter[] param = new SqlParameter[1];
param[0] = new SqlParameter(sbo, System.Data.SqlDbType.NVarChar, 4000);
param[0].Value = sbo;
objdata = SqlHelper.ExecuteDataset(ConString, CommandType.StoredProcedure, "SP_MedicineHistory", param);
return JsonConvert.SerializeObject(objdata, Newtonsoft.Json.Formatting.Indented);
//return sbo;
}
catch (SqlException ex)
{
return objSubjectReader.ToString();
}
}
谢谢。
我觉得是在抱怨参数的name;您正在传递 sbo
作为参数名称,但 sbo
是 完全组成的 TSQL,这将...很长。注意:你的 SQL 是完全不安全的——也就是说 而不是 如何参数化 SQL!
单行的正确参数化 SQL 类似于:
const string sql = @"
insert into tblMedicineHistory (PatientID, MedicineName, MedicineType, ...)
values(@patientId, @medicineName, @medicineType, ...)";
var args = new[] {
new SqlParameter("@patientId", PatientId),
new SqlParameter("@medicineName", MedicineName),
new SqlParameter("@medicineType", MedicineType),
...
}