我再c#中写程序用SQLServer2000,需要把一个TXT文本写入到数据库中,请问我怎么办,请高手帮忙比方我的txt文本里有四行:
01964315:402008052691
01964315:402008052692
01964315:402008052693
01964315:402008052694 则我写入表KQ(KQ000,KQ001)中的KQ001字段上,而KQ000则是自动递增的,其运行结果应该是:
KQ000 KQ001
1 01964315:402008052691
2 01964315:402008052692
3 01964315:402008052693
4 01964315:402008052694
怎么办,求救~~~
01964315:402008052691
01964315:402008052692
01964315:402008052693
01964315:402008052694 则我写入表KQ(KQ000,KQ001)中的KQ001字段上,而KQ000则是自动递增的,其运行结果应该是:
KQ000 KQ001
1 01964315:402008052691
2 01964315:402008052692
3 01964315:402008052693
4 01964315:402008052694
怎么办,求救~~~
FileStream filestream = new FileStream("d:\\a.txt", FileMode.Open, FileAccess.Read);
StreamReader reader= new StreamReader (filestream,Encoding .Default );
string value="";
SqlConnection conn=new SqlConnection("你的数据库连接字符串");
conn.Open();
while(!string.IsNullOrEmpty(value=reder.ReadLine()))
{
SqlCommand comm = new SqlCommand ("insert into kq Values(@file) ", conn);
SqlParameter p=new SqlParameter("@file",SqlDataType.Varchar);
p.Value=value;
comm.Parameters.Add(p);
comm.ExecuteNonQuery();
}
conn.Close();大约就是这样的 剩下的就要你自己来改动了
备份成文件还有恢复数据库的,
选择从文件恢复
然后:
private void SaveFile()
{
Form1 frm = new Form1();
FileStream filestream = new FileStream(Application.StartupPath + "\\a.txt", FileMode.Open, FileAccess.Read);
BinaryReader filerd = new BinaryReader(filestream,Encoding .Default );
byte[] filebyte = new byte[filestream.Length];
filerd.Read(filebyte, 0, (int)filestream.Length); frm.OpenConn(); OleDbCommand comm = new OleDbCommand("insert into file (id,file) Values(@fid,@file) ", frm.dbconn );
comm.Parameters.AddWithValue("@fid", "0001");
comm.Parameters.AddWithValue("@file", DBNull.Value);
comm.Parameters["@file"].Value = filebyte;
comm.ExecuteNonQuery(); }
private void ReadFile()
{
Form1 frm = new Form1();
frm.OpenConn();
OleDbCommand comm = new OleDbCommand("select * from file", frm.dbconn);
OleDbDataAdapter da = new OleDbDataAdapter(comm);
DataSet ds = new DataSet();
da.Fill(ds);
byte[] filebyte = (byte[])ds.Tables[0].Rows[0]["File"];
// System.Text.Encoding mycode = new System.Text.Encoding();
this.richTextBox1.AppendText(Encoding.Default.GetString(filebyte));
}
将字段设置为image,
OleDb换成Sql,
frm.OpenConn();frm.dbconn换成你自己的连接数据库代码.
SqlConnection cn = new SqlConnection("yourConnectionString");
string temp = reader.ReadLine();
cn.Open();
while (!string.IsNullOrEmpty(temp))
{
SqlCommand cmd = new SqlCommand("insert into KQ(KQ001) Values(@KQ001) ", cn);
cmd.Parameters.AddWithValue("@KQ001", temp);
cmd.ExecuteNonQuery();
temp = reader.ReadLine();
}
cn.Close();