C# 检查DataReader是否为空
当C# 检查DataReader是否为空,c#,visual-studio-2010,datareader,C#,Visual Studio 2010,Datareader,当数据读取器为空时,我的代码不运行。下面是我的代码 我的工作是关于日期安排。我的问题是关于假期的限制。当用户输入日期(开始日期和结束日期)时,程序将检查输入的日期之间是否有节假日。如果DataReader没有任何数据,则应保存输入的日期,或者如果DataReader有数据,则不保存输入的日期,并且程序会给出错误消息 try { econ = new SqlConnection(); econ.ConnectionString = emp_con; econ.Open();
数据读取器
为空时,我的代码不运行。下面是我的代码
我的工作是关于日期安排。我的问题是关于假期的限制。当用户输入日期(开始日期和结束日期)时,程序将检查输入的日期之间是否有节假日。如果DataReader
没有任何数据,则应保存输入的日期,或者如果DataReader
有数据,则不保存输入的日期,并且程序会给出错误消息
try
{
econ = new SqlConnection();
econ.ConnectionString = emp_con;
econ.Open();
ecmd = new SqlCommand("SELECT CD_Date FROM CONS_DATES where CD_Date between '" + Convert.ToDateTime(dtpStart.Text) + "' and '" + Convert.ToDateTime(dtpEnd.Text) + "'", econ);
ecmd.CommandType = CommandType.Text;
ecmd.Connection = econ;
dr = ecmd.ExecuteReader();
while (dr.Read())
{
DateTime cdname = (DateTime)dr["CD_Date"];
//This code is working
if (Convert.ToDateTime(cdname) >= Convert.ToDateTime(dtpStart.Text) || Convert.ToDateTime(cdname) <= Convert.ToDateTime(dtpEnd.Text))
{
MessageBox.Show("Holiday Constraint. Creating Record Denied.");
} //if
//This code is not working. When the program fetch with no record, it should be continue to add the record but it's not working
else
if (dr == null || !dr.HasRows)
{
//In this area is my code for inserting the entered data.
MessageBox.Show("Add na|!!!. Creating Record Denied.");
}//if else
}//while
}//try
catch (Exception x)
{
MessageBox.Show(x.GetBaseException().ToString(), "Connection Status", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
试试看
{
econ=newsqlconnection();
经济连接字符串=emp_con;
经济开放();
ecmd=new SqlCommand(“从CONS_DATES中选择CD_Date,其中CD_Date介于“+Convert.ToDateTime(dtpStart.Text)+”和“+Convert.ToDateTime(dtpEnd.Text)+”之间,econ”);
ecmd.CommandType=CommandType.Text;
ecmd.Connection=econ;
dr=ecmd.ExecuteReader();
while(dr.Read())
{
DateTime cdname=(DateTime)dr[“CD_Date”];
//此代码正在运行
如果(Convert.ToDateTime(cdname)>=Convert.ToDateTime(dtpStart.Text)| Convert.ToDateTime(cdname)而(dr.Read())
表示dr至少有一行
所以其他人永远不会执行
您可以在while之前执行其他条件吗?您的问题是,只有当dr
有一个或多个记录时,while
循环才会运行。但是,如果dr
为null
则while
循环将永远不会运行
更好的解决方案是使用System.Data.SqlClient.SqlDataReader
检查一下
if (!dr.HasRows)
{
// Your code to save the records, if no holidays found
}
else
{
// Your code to show the error message, if there is one or more holidays
}
我尝试了我的想法,它回答了我的问题。我所做的是在datagridview中显示提取的数据,然后我创建了一个条件如果datagridview有记录,那么我的记录将不会被保存,如果datagridview没有任何记录,那么记录将被保存
谢谢大家!试试这个:
while (sqlreader.Read())
{
tbSubscriptionInfo.Text = sqlreader["nr_ore"].ToString();
}
if (!sqlreader.HasRows)
{
tbSubscriptionInfo.Text = "";
}
您希望如何使用DataReader(一个读取数据的对象)添加记录?这就是您所有的代码吗?因为while循环的条件是dr.Read()
,dr
在循环中不能null
。如果它是null
,而循环将根本不执行。通过检查dr
中的null
,你想实现什么?@Markust,我没有使用DataReader保存我的记录,我只是使用DataReader获取我的数据。我上面的代码不完整,有些代码不属于这个问题,它们都在工作。@silent_warrior,现在我了解到Read博士()不能为空,我真的想检查值。可能重复我应该在哪里添加System.Data.SqlClient.SqlDataReader?谢谢dr
应该是System.Data.SqlClient.SqlDataReader
,我假设您使用的是Microsoft.VisualStudio.Data.DataReader
。如果不需要定义任何内容,请使用t上面的代码。什么不起作用?请具体说明。你想实现什么?谢谢你帮我想出一个主意。我只需传递要在datagridview中显示的数据,然后创建一个条件。如果datagridview有值,记录将不会保存。如果记录没有任何记录,输入的数据将被保存。如果你不需要要向用户显示datagridview,只需在我的回答中使用if条件来保存记录overhead@silent_warrior,谢谢。我记得。
if (dr == null || !dr.HasRows)
{
// Your code to show the error message, if there is one or more holidays
}
else
{
// Your code to save the records, if no holidays found
}