插入日期到SQL Server 2000 [英] inserting date to sql server 2000

查看:102
本文介绍了插入日期到SQL Server 2000的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我在将日期保存在sql server 2000中时遇到一些问题.
从我的应用程序中,我以日期格式"mm/dd/yyyy"-"07-11-2011"保存了数据.我检查了它是否将这种格式的数据发送到sql服务器.

但是当我在sql server中签入时,它会另存为"2011-11-07 00:00:00.000"这种格式.

当我从报告中检索该问题时,其标题为"2011-yyyy",11-mm","07-dd",该怎么办?

在这种情况下请任何人帮助我...

以下是代码....


< b>添加到sql server</b>
< pre>公共无效AddTimesheet(DateTime Dtpicker,int cmbEmpno,string txtEmpname,string txtLocation,string cmbMode,string cmbOtmode,int txtNormalOT,int txtDayofOT)
{
字符串sqlstr;
sqlstr =" INSERT INTO Master(cDate,Emp_Number,Emp_Name,Work_Location,Mode,OTMode,Normal_OT,DayOff_OT)& quot; +
& quot;值(&#39;& + Dtpicker +&&#39;,&" + cmbEmpno +&,&#39;&" + txtEmpname +& quot;&#39;&#39; amp; txtLocation +& quot;&#39;&#39; amp; quot; + cmbMode +& quot;' ;,&#39;& + cmbOtmode +&&#39;,&" + txtNormalOT +&,&" + txtDayofOT +&)& quot ;;
con =新的SqlConnection(clsConnection.strconnection);
con.Open();

da =新的SqlDataAdapter();
da.InsertCommand =新的SqlCommand();
da.InsertCommand.Connection = con;
da.InsertCommand.CommandText = sqlstr;
da.InsertCommand.CommandType = CommandType.Text;
da.InsertCommand.ExecuteNonQuery();

DateTime datetimepic = Convert.ToDateTime(Dtpicker.Value.Date);
int empno = Convert.ToInt32(cmbEmpno.Text);
字符串empname = txtEmpname.Text;
字符串emplocation = txtLocation.Text;
字符串模式= cmbMode.Text;
字符串otmode = cmbOtmode.Text;
int normalot = Convert.ToInt32(txtNormalOT.Text);
int dayoffot = Convert.ToInt32(txtDayofOT.Text);

Classes.clsAttendance att =新测试.Classes.clsAttendance();
att.AddTimesheet(datetimepic,empno,empname,emplocation,mode,otmode,normalot,dayoffot);</pre>

< b>报告代码</b>

< pre> DateTime Datefrom = Convert.ToDateTime(dateFrom.Value.Date);
DateTime Dateto = Convert.ToDateTime(dateTo.Text);
字符串Location1 = Location.Text;
字符串Mode1 = Mode.Text;
字符串Otmode = otmode.Text;

字符串sqlstr =& quot; SELECT * FROM Master ORDER BY Emp_Number& quot ;;
con =新的SqlConnection(clsConnection.strconnection);
con.Open();
DataSet ds = new DtReport();
DataTable dt = new DataTable();
SqlDataAdapter da =默认值(SqlDataAdapter);

da =新的SqlDataAdapter(sqlstr,con);
da.Fill(dt);
foreach(dt.Rows中的DataRow行)
{
ds.Tables [&"Master&"].ImportRow(row);
}
rptreport objreport =新的rptreport();
objreport.SetDataSource(ds);

objreport.SetParameterValue(&"Datefrom&",dateFrom.Value.Date);
objreport.SetParameterValue(&"Dateto&",dateTo.Text);
objreport.SetParameterValue(& quot; Location& quot ;, Location1);
objreport.SetParameterValue(& quot; Mode& quot; Mode1);
objreport.SetParameterValue(&"Otmode&",Otmode);

crystalReportViewer1.ReportSource = objreport;
crystalReportViewer1.Show();</pre>

hi all,

i am having some issues in saving the date in sql server 2000.
from my application i have save the data with date format "mm/dd/yyyy"-"07-11-2011". I have checked it''s sending the data with this format to the sql server.

but when i check in the sql server it save as "2011-11-07 00:00:00.000" this format.

when i''m retrieving from the report it comes as "2011-yyyy",11-mm"","07-dd" for this issue what can i do?

please any one help me in this case...

below are the codes....


<b>add to the sql server</b>
<pre>public void AddTimesheet(DateTime Dtpicker, int cmbEmpno, string txtEmpname, string txtLocation, string cmbMode, string cmbOtmode, int txtNormalOT, int txtDayofOT)
{
string sqlstr;
sqlstr = &quot;INSERT INTO Master (cDate,Emp_Number,Emp_Name,Work_Location,Mode,OTMode,Normal_OT,DayOff_OT)&quot; +
&quot; VALUES ( &#39;&quot; + Dtpicker + &quot;&#39;,&quot; + cmbEmpno + &quot;, &#39;&quot; + txtEmpname + &quot;&#39;,&#39;&quot; + txtLocation + &quot;&#39;,&#39;&quot; + cmbMode + &quot;&#39;, &#39;&quot; + cmbOtmode + &quot;&#39;,&quot; + txtNormalOT + &quot;,&quot; + txtDayofOT + &quot;)&quot;;
con = new SqlConnection(clsConnection.strconnection);
con.Open();

da = new SqlDataAdapter();
da.InsertCommand = new SqlCommand();
da.InsertCommand.Connection = con;
da.InsertCommand.CommandText = sqlstr;
da.InsertCommand.CommandType = CommandType.Text;
da.InsertCommand.ExecuteNonQuery();

DateTime datetimepic = Convert.ToDateTime(Dtpicker.Value.Date);
int empno = Convert.ToInt32(cmbEmpno.Text);
string empname = txtEmpname.Text;
string emplocation = txtLocation.Text;
string mode = cmbMode.Text;
string otmode = cmbOtmode.Text;
int normalot = Convert.ToInt32(txtNormalOT.Text);
int dayoffot = Convert.ToInt32(txtDayofOT.Text);

Classes.clsAttendance att = new testing.Classes.clsAttendance();
att.AddTimesheet(datetimepic, empno, empname, emplocation, mode, otmode, normalot, dayoffot);</pre>

<b>report code</b>

<pre>DateTime Datefrom = Convert.ToDateTime(dateFrom.Value.Date);
DateTime Dateto =Convert.ToDateTime( dateTo.Text);
string Location1 = Location.Text;
string Mode1 = Mode.Text;
string Otmode = otmode.Text;

string sqlstr = &quot;SELECT * FROM Master ORDER BY Emp_Number&quot;;
con = new SqlConnection(clsConnection.strconnection);
con.Open();
DataSet ds = new DtReport();
DataTable dt = new DataTable();
SqlDataAdapter da = default(SqlDataAdapter);

da = new SqlDataAdapter(sqlstr, con);
da.Fill(dt);
foreach (DataRow row in dt.Rows)
{
ds.Tables[&quot;Master&quot;].ImportRow(row);
}
rptreport objreport = new rptreport();
objreport.SetDataSource(ds);

objreport.SetParameterValue(&quot;Datefrom&quot;, dateFrom.Value.Date);
objreport.SetParameterValue(&quot;Dateto&quot;, dateTo.Text);
objreport.SetParameterValue(&quot;Location&quot;, Location1);
objreport.SetParameterValue(&quot;Mode&quot;, Mode1);
objreport.SetParameterValue(&quot;Otmode&quot;, Otmode);

crystalReportViewer1.ReportSource = objreport;
crystalReportViewer1.Show();</pre>

推荐答案

Try this query to get records from database
SELECT eventID,eventName, eventPlace,eventTime,CONVERT (varchar(150), eventDate,<u>103</u>) As eventDate
FROM         EventTimeTable

For more Date Format please follow this link

http://msdn.microsoft.com/en-us/library/ms187928.aspx


这篇关于插入日期到SQL Server 2000的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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