char '00' 未从 c# 中的 SQL 服务器转换为 int 01
char '00' not converting to int 01 from SQL Server in c#
我正在从 char(2) 数据类型的数据库中获取一列。
在事件中,我将 char 数据类型更改为 int 并将其增加 1。
通过以下代码:
int i = 0;
using (SqlConnection sqlCon = new SqlConnection(Login.connectionString))
{
string commandString = "SELECT MAX(CAST(Category_Code as INT)) FROM Category;";
SqlCommand sqlCmd = new SqlCommand(commandString, sqlCon);
sqlCon.Open();
SqlDataReader dr = sqlCmd.ExecuteReader();
while (dr.Read())
{
i = 1;
if(dr[0] == null)
{
Ctgry_CtgryCodeCb.Text = "1";
}
else
{
int cat_next_code = int.Parse(dr[0].ToString());
Ctgry_CtgryCodeCb.Text = (cat_next_code + 1).ToString();
}
它工作正常但不是第一次(不加 1 到空或 0)因为列是 empty.It 显示一些错误,它不是 convert.I 的正确格式还将列的默认值设置为 0,但它显示 ((0)) 作为默认值。
任何帮助将不胜感激。
如果您使用此代码来增加数据库的主键值 table,您不应该这样做。您应该使用数据库中可用的 IDENTITY
列功能。
由于您没有解释为什么不使用 IDENTITY
列,看起来这段代码有其他用途。
根据您的代码,您正在从数据库中获取某些列的最大值,并在代码中将其递增 1。
当数据库中的table为空时你什么也得不到reader。所以while循环根本不会执行。因此,即使您在 while 循环中检查 NullOrEmpty,它也永远不会被执行。
另外你不需要在这里使用SqlDataReader
。由于您只从查询中返回一个值,因此您可以使用 SqlCommand
的 ExecuteScalar
方法并获取该值。会更简单。
var codeFromDb = sqlCmd.ExecuteScalar();
var cat_next_code = 0;
if(!(codeFromDb is DBNull))
{
cat_next_code = Convert.ToInt32(codeFromDb);
}
Ctgry_CtgryCodeCb.Text = (cat_next_code + 1).ToString();
我强烈建议使用 IDENTITY
列而不是执行所有这些代码。
这将帮助您解决当前的问题。
SqlDataReader
在这种情况下有点矫枉过正,我不想回答错误的方法,但既然你坚持考虑以下。
SqlDataReader dr = sqlCmd.ExecuteReader();
int cat_next_code = 0;
if(dr.Read()) // while is not needed here. There will be only one row in the reader as per the query.
{
i = 1;
if(!dr.IsDBNull(0))
{
cat_next_code = int.Parse(dr[0].ToString());
}
}
Ctgry_CtgryCodeCb.Text = (cat_next_code + 1).ToString();
我正在从 char(2) 数据类型的数据库中获取一列。 在事件中,我将 char 数据类型更改为 int 并将其增加 1。 通过以下代码:
int i = 0;
using (SqlConnection sqlCon = new SqlConnection(Login.connectionString))
{
string commandString = "SELECT MAX(CAST(Category_Code as INT)) FROM Category;";
SqlCommand sqlCmd = new SqlCommand(commandString, sqlCon);
sqlCon.Open();
SqlDataReader dr = sqlCmd.ExecuteReader();
while (dr.Read())
{
i = 1;
if(dr[0] == null)
{
Ctgry_CtgryCodeCb.Text = "1";
}
else
{
int cat_next_code = int.Parse(dr[0].ToString());
Ctgry_CtgryCodeCb.Text = (cat_next_code + 1).ToString();
}
它工作正常但不是第一次(不加 1 到空或 0)因为列是 empty.It 显示一些错误,它不是 convert.I 的正确格式还将列的默认值设置为 0,但它显示 ((0)) 作为默认值。 任何帮助将不胜感激。
如果您使用此代码来增加数据库的主键值 table,您不应该这样做。您应该使用数据库中可用的 IDENTITY
列功能。
由于您没有解释为什么不使用 IDENTITY
列,看起来这段代码有其他用途。
根据您的代码,您正在从数据库中获取某些列的最大值,并在代码中将其递增 1。
当数据库中的table为空时你什么也得不到reader。所以while循环根本不会执行。因此,即使您在 while 循环中检查 NullOrEmpty,它也永远不会被执行。
另外你不需要在这里使用SqlDataReader
。由于您只从查询中返回一个值,因此您可以使用 SqlCommand
的 ExecuteScalar
方法并获取该值。会更简单。
var codeFromDb = sqlCmd.ExecuteScalar();
var cat_next_code = 0;
if(!(codeFromDb is DBNull))
{
cat_next_code = Convert.ToInt32(codeFromDb);
}
Ctgry_CtgryCodeCb.Text = (cat_next_code + 1).ToString();
我强烈建议使用 IDENTITY
列而不是执行所有这些代码。
这将帮助您解决当前的问题。
SqlDataReader
在这种情况下有点矫枉过正,我不想回答错误的方法,但既然你坚持考虑以下。
SqlDataReader dr = sqlCmd.ExecuteReader();
int cat_next_code = 0;
if(dr.Read()) // while is not needed here. There will be only one row in the reader as per the query.
{
i = 1;
if(!dr.IsDBNull(0))
{
cat_next_code = int.Parse(dr[0].ToString());
}
}
Ctgry_CtgryCodeCb.Text = (cat_next_code + 1).ToString();