使用sql批量数据类型转换的问题 [英] problem in using sql bulk dataypes convertion
问题描述
DataTable dt = new DataTable();
dt.Columns.Add("L_DateTime");
dt.Columns.Add("L_Direction");
dt.Columns.Add("L_CardID");
dt.Columns.Add("L_GateID");
int i2 = 0;
while ((inputLine = rdr.ReadLine()) != null) //Read while
//the line is not null
{
string[] arr;
arr = inputLine.Split('',''); //splitting the line which
//was read by the stream reader object
row = dt.NewRow();
row["L_DateTime"] = Convert.ToDateTime(arr[0]);
row["L_Direction"] = arr[1];
row["L_CardID"] = arr[2];
row["L_GateID"] = Convert.ToInt32(arr[3]);
dt.Rows.Add(row);
i2++;
}
sqlbulkCopy.WriteToServer(dt);
sqlbulkCopy.Close();
问候大家...我在这里有问题...
让我解释一下这段代码的全部含义...
即时通讯使用sqlbulk从文本文件读取数据,并将其存储在数据库中.据即时通讯在文本文件中,数据都被视为字符串(也许)!..
在我的数据库的前面,我创建了名称与此代码完全相同的行..
首先在我的sql dbase中,我将所有数据都视为字符串..并且工作正常..
但是由于涉及日期时间...我将其更改为合适的类型...
和我努力得到错误...说:不能被识别为有效的日期时间!!!
我不明白...我已经将其转换为日期时间...任何建议都将是不错的....对于所有编码人员都如此!
Greeting everyone...i have problem here...
let me explain a little bit what this code is all about...
im using sqlbulk to read data from text file, and store it in database. As far as im aware in text file, the data all treated as string(perhaps)!..
earlier in my database i have created rows which name are exactly as in this code..
first in my sql dbase, im treated all data as string..and it is working fine..
but since it involving datetime...i change it to suitable type...
and i strting to get error...said :STRING WAS NOT RECOGNIZED AS A VALID DATETIME!!!
i dont understand...i have already converted it to datetime...any suggestion would be nice....regards to all coders!!
推荐答案
我相信您正在检索的L_DateTime
的格式对于DateTime
无效.使用DateTime.TryParse
方法而不是直接转换值,或者使用.ToString()
将其另存为string
格式.
问候,
爱德华(Eduard)
I believe the format ofL_DateTime
you are retrieving is not valid forDateTime
. UseDateTime.TryParse
method instead of directly converting the values or, save it asstring
format by using.ToString()
.
Regards,
Eduard
代替Convert.ToDateTime
使用DateTime.TryParse
.
在这种情况下,如果输入的日期时间无效,则不会收到此错误.
要了解有关DateTime.TryParse
方法的更多信息,请通过此处 [ ^ ].
Instead ofConvert.ToDateTime
useDateTime.TryParse
.
In this case, if your input is not a valid date time, you will not get this error.
To know more about theDateTime.TryParse
method, go through here[^].
这篇关于使用sql批量数据类型转换的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!