C# 尝试将字母数字值插入数据库时列名无效
基本上,我有一个代码,它接受(1)个字母数字或(2)个数字序列号并递增它们。所有操作都适用于数字序列号,但当我尝试插入字母数字序列号时,会出现“无效列名”错误 我在这里看过很多“无效列名”的帖子,它们似乎都不能回答我的问题。我已经设置了断点,并为这两种情况(数字和字母数字)运行了代码,得到了相同的数据类型。基本上,所有的东西都排对了,所以我不知所措 下面的代码显示了在这两种情况下我是如何递增的。请注意,对于字母数字增量,我调用了方法IncrementAlphaNumeric,它接受变量“Output”,这是对表进行排序并获取最后一个序列号的SQL查询的结果C# 尝试将字母数字值插入数据库时列名无效,c#,sql,C#,Sql,基本上,我有一个代码,它接受(1)个字母数字或(2)个数字序列号并递增它们。所有操作都适用于数字序列号,但当我尝试插入字母数字序列号时,会出现“无效列名”错误 我在这里看过很多“无效列名”的帖子,它们似乎都不能回答我的问题。我已经设置了断点,并为这两种情况(数字和字母数字)运行了代码,得到了相同的数据类型。基本上,所有的东西都排对了,所以我不知所措 下面的代码显示了在这两种情况下我是如何递增的。请注意,对于字母数字增量,我调用了方法IncrementAlphaNumeric,它接受变量“Outp
// Increment Numeric Serial Numbers
if (isNum)
{
int lastNumber = Int32.Parse(Output);
int[] ints = Enumerable.Range(lastNumber + 1, printQuantity).Select(i => (int)i / 1).ToArray();
increments = ints.Select(x => x.ToString()).ToArray();
output.AppendText("Serial numbers to print: " + string.Join(", ", increments));
}
// Increment AlphaNumeric Serial Numbers
if (!isNum)
{
for (int i = 0; i < printQuantity; i++)
{
increments[i] = IncrementAlphaNumeric(Output);
snList.Add(increments[i]);
Output = increments[i];
}
output.AppendText("Serial numbers to print: " + string.Join(", ", increments));
}
同样,它适用于数字,但不适用于字母数字。当我放入断点并逐步遍历代码时,每种情况下的数据类型(字符串)都是相同的,数字和字母数字
我得到的错误消息也是“无效列名”。基本上,预期的结果应该是,序列号,无论是数字还是字母数字,都应该插入到数据库的正确表中,该表基于设备类型(dType)。如注释中所述,您不应该连接字符串来构建sql语句。
在您的例子中,我假设您希望使用一条语句在数据库中插入多条记录。也可以使用参数和手动构建值部分或查询来完成此操作(此语法可从Sql Server 2008获得)
//示例值,用构建增量数组的代码替换它们
字符串[]增量=新字符串[]{“VALUE1”、“VALUE2”、“VALUE3”、“VALUE4”};
//查询的不变部分
string baseQuery=“插入[Manufacturing].[dbo].[Device.Devices]([SerialNumber],[DeviceTypeID])值”;
//类型的固定值
字符串dType=“42”;
List prms=新列表();
列表占位符=新列表();
//构建参数占位符列表以及这些参数及其值的列表
对于(int x=0;x
多次询问。。。这是因为SQL中的字符串文本需要“
。。。但是您应该使用,以避免我不是C用户,但是对于文本值插入失败的原因,解释是SQL中的字符串文本采用单引号,例如,'data'
,而不是data
。但是,不要试图用引号括住数据。相反,要学习如何使用C#代码中准备好的语句。您还存在多个insert命令的问题。它们需要在每个INSERT语句的末尾用分号分隔。感谢您的快速回复。我对C#/SQL还是有点陌生。foreach/StringBuilder语法将是我参数化插入查询的地方?谢谢您的帮助。我现在明白了为什么要参数化SQL查询,这也是我最终要做的。我只是想启动并运行业务逻辑,以测试我正在创建的程序的有效性。
// (5) Store new SNs in Database
StringBuilder sb = new StringBuilder();
foreach (string newSns in increments)
{
sb.AppendLine("INSERT INTO [Manufacturing].[dbo].[Device.Devices]([SerialNumber],[DeviceTypeID]) VALUES(" + newSns + "," + dType +")");
}
using (SqlCommand insertCommand = new SqlCommand(sb.ToString(), cnn))
{
var executeNonQuery = insertCommand.ExecuteNonQuery();
}
// Sample values, replace them with your code that builds the increments array
string[] increments = new string[] {"VALUE1", "VALUE2","VALUE3", "VALUE4"};
// Invariant part of your query
string baseQuery = "INSERT INTO [Manufacturing].[dbo].[Device.Devices]([SerialNumber],[DeviceTypeID]) VALUES";
// Fixed value for the type
string dType = "42";
List<SqlParameter> prms = new List<SqlParameter>();
List<string> placeHolders = new List<String>();
// Build a list of parameter placeholders and a list of those parameter and their values
for(int x = 0; x < increments.Length; x++)
{
placeHolders.Add($"(@p{x},{dType})");
prms.Add(new SqlParameter { ParameterName = $"@p{x}", SqlDbType = SqlDbType.NVarChar, Value = increments[x]});
}
// Put the text together
string queryText = baseQuery + string.Join(",", placeHolders);
// This should be the final text
// INSERT INTO [Manufacturing].[dbo].[Device.Devices]([SerialNumber],[DeviceTypeID])
// VALUES(@p0,42),(@p1,42),(@p2,42),(@p3,42)
using (SqlCommand insertCommand = new SqlCommand(queryText, cnn))
{
// Add all parameters to the command...
insertCommand.Parameters.AddRange(prms.ToArray());
var executeNonQuery = insertCommand.ExecuteNonQuery();
}