//检索数据表获得当前列的最大值
public int GetMaxDataBase(string tempStrSQL)
{
int max = 0;
this.myConnection = new SqlConnection(connectionString);
SqlCommand tempSqlCommand = new SqlCommand(tempStrSQL, this.myConnection);
try
{
myConnection.Open();
if (tempSqlCommand.ExecuteScalar().ToString() != null)
{
max = int.Parse(tempSqlCommand.ExecuteScalar().ToString());这里出错了,tempSqlCommand.ExecuteScalar()执行的结果为空,没有记录,请大家指点,我该怎么做呢,谢谢 }
else
{
max = 0;
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message); }
return max;
}
public int GetMaxDataBase(string tempStrSQL)
{
int max = 0;
this.myConnection = new SqlConnection(connectionString);
SqlCommand tempSqlCommand = new SqlCommand(tempStrSQL, this.myConnection);
try
{
myConnection.Open();
if (tempSqlCommand.ExecuteScalar().ToString() != null)
{
max = int.Parse(tempSqlCommand.ExecuteScalar().ToString());这里出错了,tempSqlCommand.ExecuteScalar()执行的结果为空,没有记录,请大家指点,我该怎么做呢,谢谢 }
else
{
max = 0;
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message); }
return max;
}
解决方案 »
- 求正则匹配一个网页
- jquery执行iframe事件问题
- 求读数据库中的2进制流(文件).并把文件保存到本地的代码
- DataGrid绑定更新后数据时跳过更新行的问题,请指教
- 关于Alter语句的问题
- 各位大虾帮帮忙说下"解题"的思路
- Find_Form是MainForm的弹出窗口,请问我如何让Find_Form显示在当前win的最上面。
- ●●●好像属于是入门问题,但是你未必知道:这种C++结构在C#中怎么声明?●●●
- 如何从一个 实体 复制到 另一个相同结构的实体
- 各位老大!有没有用C#写的浏览器的程序?
- 写了个服务程序,但是部署安装的时候老是跳出输入“服务用户登录“
- WinForm里的DateGrid获取行,列的语句怎么写?
public int GetMaxDataBase(string tempStrSQL)
{
int max = 0;
this.myConnection = new SqlConnection(connectionString);
SqlCommand tempSqlCommand = new SqlCommand(tempStrSQL, this.myConnection);
try
{
myConnection.Open();
if (tempSqlCommand.ExecuteScalar().ToString() != String.Empty)
{
max = int.Parse(tempSqlCommand.ExecuteScalar().ToString());
}
else
{
max = 0;
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message); }
return max;
}
问题解决了,但我不明白,把tempSqlCommand.ExecuteScalar().ToString() != String.Empty写成tempSqlCommand.ExecuteScalar().ToString() != null为什么不对呢?数据库中的查询结果可是null
DBNull表示是数据表中查询的内容是空的
String.Empty表示一个空字符串。
tempSqlCommand.ExecuteScalar()的结果是object,这样写出错了呀
if(o != null)
{
...
}