如何从查询中传递datetime参数? [英] How pass datetime parameter from query?

查看:70
本文介绍了如何从查询中传递datetime参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将我的日期时间从数据库变为变量datetimedat,但是,当前代码抛出了一个编译错误--->使用未分配的局部变量'dat'

  public   static   string  content()
{
string xml = ;
string title = ;
string content = ;
DateTime dat = new DateTime();

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings [ #######]的ConnectionString)。
string commandtext = #### #######;
SqlCommand command = new SqlCommand(commandtext,con);
con.Open();
command.Parameters.Add( new SqlParameter( title,title));
command.Parameters.Add( new SqlParameter( 身体,内容));
command.Parameters.Add( new SqlParameter( ACTIVEDATE,dat));

SqlDataReader reader = command.ExecuteReader();

dat.ToString( dd-MM-yyyy);

// XElement代码

while (reader.Read())
{
string top = reader.GetString( 0 );
string dtt = reader.GetString( 1 );
string body = reader.GetString( 2 );

var result = top + < br /> + dtt + < br /> + body + < br />;

string data = result.ToString();

xeSendTo1.Add( new XElement( value,( new XCData(data))));

}
con.Close();

return xml = xDoc.ToString();
}



我尝试将赋值语句更改为以下内容,但它们进一步导致更多编译错误。



 DateTime dat = new DateTime();< br /> 
< br />
DateTime.TryParseExact(...)





是为sqlParameter声明Datetime变量的正确方法。



请咨询。



非常感谢

解决方案

引用:

dat.ToString(dd-MM-yyyy);

是问题所在。它没有做任何事情。



删除该代码并在你的while循环中执行:



< pre lang =c#> dat = DateTime.Parse(reader [ dateField]。ToString( ));





或者甚至使用TryParse更准确。


引用:

使用未分配的局部变量'dat'

该消息足够清晰。您正在尝试使用尚未分配的内容。这是 dat



所以,给它分配一些值。然后阅读。



我想你应该像Ryan建议的那样读取while循环中的数据库DateTime字段。


I am trying to get my datetime from database, into the variable datetime "dat", however, the current code throws a compiling error of ---> Use of unassigned local variable 'dat'

public static string content()
       {
           string xml = "";
           string title = "";
           string content = "";
           DateTime dat = new DateTime();

           SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["#######"].ConnectionString);
           string commandtext = "###########";
           SqlCommand command = new SqlCommand(commandtext, con);
           con.Open();
           command.Parameters.Add(new SqlParameter("title", title));
           command.Parameters.Add(new SqlParameter("Body", content));
           command.Parameters.Add(new SqlParameter("ACTIVEDATE", dat));

           SqlDataReader reader = command.ExecuteReader();

           dat.ToString("dd-MM-yyyy");

           //XElement code

           while (reader.Read())
           {
               string top = reader.GetString(0);
               string dtt = reader.GetString(1);
               string body = reader.GetString(2);

               var result = top + "<br />" + dtt + "<br />" + body + "<br />";

               string data = result.ToString();

               xeSendTo1.Add(new XElement("value", (new XCData(data))));

           }
           con.Close();

           return xml = xDoc.ToString();
       }


I tried changing the assignment statement to the following, but they further resulted in more compiling errors.

DateTime dat = new DateTime();<br />
<br />
DateTime.TryParseExact(...)



is their a correct approach to declare the Datetime variable, for the sqlParameter.

Please advice.

Many thanks

解决方案

Quote:

dat.ToString("dd-MM-yyyy");

Is the problem. It doesn't do anything.

Remove that code and inside your while loop do:

dat = DateTime.Parse(reader["dateField"].ToString());



Or even use TryParse to be more accurate.


Quote:

Use of unassigned local variable 'dat'

The message is clear enough. You are trying to use something, which is not assigned yet. Here is "dat".

So, assign some value to it. Then read.

I guess you should read the database DateTime field inside the while loop as Ryan suggested.


这篇关于如何从查询中传递datetime参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆