请教我如何使用Linq将循环ddta插入数据库 [英] Please teach me how to insert loop ddta to database with Linq
问题描述
能否请您帮忙:我导入文本文件并想插入数据库
像这样的数据12345120520110800I共5000条记录.
12345 =员工代码,12052011 =日期时间,0800 =员工时钟时间,I =进/出
像这样轻松创建数据库:id(bigint)自动递增Emp_code(varchar5)clock(datetime)IO(varchar 2)
我的代码如下:
Could you please help me : I import textfile and would like to insert to database
The data like this 12345120520110800I total 5000 record.
12345 = employee code , 12052011 = datetime , 0800 = employee clock time and I = In/Out
database easy create like this: id(bigint) auto increment Emp_code(varchar5) clock(datetime) IO(varchar 2)
my code as below :
private void btnOpenRead_Click(object sender, EventArgs e)
{
try
{
openFileDialog1.FileName = "";
openFileDialog1.Filter = "Text Files(*.txt)|*.txt";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
label1.Text = openFileDialog1.FileName;
StreamReader sr = new StreamReader(openFileDialog1.FileName);
ClockRecord = sr.ReadLine();
while (ClockRecord != "")
{
day = ConvertNumber(ClockRecord.Substring(5, 2));
month = ConvertNumber(ClockRecord.Substring(7, 2));
year = ConvertNumber(ClockRecord.Substring(9, 4));
hour = ConvertNumber(ClockRecord.Substring(13, 2));
minute = ConvertNumber(ClockRecord.Substring(15, 2));
dt = new DateTime(year, month, day, hour, minute, 0);
ClockRecord = sr.ReadLine();
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
static int ConvertNumber(string text)
{
int i;
if(Int32.TryParse(text,out i))
return i;
MessageBox.Show("Invalid data: ", text);
return 0;
}
______________________
我不知道如何写数据库.
例如,我尝试:(Linq)
______________________
I don''t know how to write to database .
for example I try : (Linq)
Times t = new Times();
t.Emp_Code = ClockRecord.Readline.Substring(5,2);
这将仅将一条记录写入数据库.我不知道该怎么做.由于没有行数.
专家请帮我..
庞(泰国)
[edit]添加了代码块,缩进得到了改善(实际上是缩进了一些)-OriginalGriff [/edit]
This will write to database only one record. I don''t know how to make a loop . Since do not have line to count .
Expert help me please ..
Pang (Thailand)
[edit]Code block added, indentation improved (i.e. actually there, some of it) - OriginalGriff[/edit]
推荐答案
如果您创建了数据库,那么您就可以了"需要将DBML文件添加到您的项目中.完成此操作后,编译您的应用程序(Shift + CTRL + B),您将拥有所需的DataContext.
您需要在循环之前 创建数据上下文.
您可以在循环中 的时间表中添加记录.
最后,在循环后 上,在数据上下文上调用SubmitChanges.
作为一种快捷方式,您可以在循环中执行以下操作以将记录添加到数据上下文中:
If you have your database created you''re going to need to add a DBML file to your project. After doing this, compile your app (Shift + CTRL + B) and you''ll have the DataContext needed.
You need to create a datacontext before your loop.
You add records to the times table during your loop.
Finally, you call SubmitChanges on your datacontext after your loop.
As a shortcut, you can do something like this in your loop to add records to the data context:
context.Times.Add(new Time{
day = ConvertNumber(ClockRecord.Substring(5, 2)),
month = ConvertNumber(ClockRecord.Substring(7, 2))
...
});
这将在线创建新对象并将其添加到上下文中.您仍然需要在循环后调用SubmitChanges.
这是良好的介绍 [ ^ ]转换为LINQ to SQL.
干杯.
This creates the new object in line and adds it to the context. You will still need to call SubmitChanges after the loop.
Here''s good intro[^] to LINQ to SQL for you.
Cheers.
谢谢,他们CallMeMrJames,
我的问题是我不知道如何进行循环(int count = 1; count<?; count ++)
原因,我从文本文件中读取文件,文本文件中有5000条记录.
我使用streamreader进行阅读.例如,一旦我尝试计算.
字符串ClockRecord = sr.ReadLine().Count();
结果是该行中char的总数,但我需要该行的编号.
为了计数.
那就是为什么我不知道如何用Linq将流文件插入数据库.
帮助我PLS ...
Thank , TheyCallMeMrJames ,
My problem is I don''t know how to do a loop like (int count=1;count < ? ;count ++)
Cause , I read the file from textfile there are 5000 record inside the textfile.
I use the streamreader to read . Once I try to count for example.
string ClockRecord = sr.ReadLine().Count();
The result is the total of char in the line but I need the number of the line.
In order to count.
That why I don''t know how to insert the stream file into database by Linq.
HELP ME PLS......
这篇关于请教我如何使用Linq将循环ddta插入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!