帮我:-读取字符串frm文本文件&存入BD [英] help me:-read string frm text file & store into BD

查看:63
本文介绍了帮我:-读取字符串frm文本文件&存入BD的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

帮我解决这个问题
---------------------------------
我必须阅读字符串文本文件,并用逗号将其分割,最后将其保存到数据库中. 例如:-在文本文件中:suhas,kumar,55555,male,22.
我必须将此数据存储到数据库中...
然后再次阅读下一行....


Help me to solve this problm
---------------------------------
i have to read string text file and split it by comma and finaly save it into database.....
ex:--In text file : suhas,kumar,55555,male,22.
i have to store this data into database...
then again read next line....


private void split()
{

    StreamReader rd1 = new StreamReader("c:\\temp1.txt");//string is suhas,kumar,8888888888,male.
    string str12 = rd1.ReadLine();
    string[] str1 = str12.Split('','');
    // splitmsg(str1);
    foreach (string part in str1)
    {
       //display array string
        MessageBox.Show(part.ToString());

    }
}



如何在插入查询中使用值..



how can i use values in insert query..

推荐答案

简单!假设您要将分割后的每个字符串放在单独的行中,那么只需要一个循环:
Easy! Assuming you want to put each string that you have split on a separate row, then you just need a loop:
foreach (string s in str1)
   {
   using (SqlCommand cmd = new SqlCommand("INSERT INTO myTable (myColumnName) VALUES (@STR)", con))
      {
      cmd.Parameters.AddWithValue("@STR", s);
      cmd.ExecuteNonQuery();
      }
   }

您可以在循环外使用SqlCommand来执行此操作,这样会效率更高,但这是读者的练习-这仅显示了一般原理.

顺便说一句:您不需要在已经知道是字符串的对象上调用ToString:它没有做任何有用的事情,只是看起来像您不在乎...

You could do this with the SqlCommand outside the loop, and it would be a lot more efficient, but that is an exercise for the reader - this just shows the general principle.

BTW: you don''t need to call ToString on objects you already know are strings: it doesn''t do anything useful and just looks like you don''t care...


但是我需要在不同的栏中...
像---> Fname Lname Phno性别
suhas kumar 55555男性
ravi kumar 5555男性
But i need that in different columns...
like---> Fname Lname Phno Gender
suhas kumar 55555 male
ravi kumar 5555 male


这篇关于帮我:-读取字符串frm文本文件&存入BD的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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