如何在SQL SERVER 2008中读取文本文件 [英] How to read text file in SQL SERVER 2008

查看:211
本文介绍了如何在SQL SERVER 2008中读取文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在sql server 2008中读取文本文件数据???



我有一个文本文件和数据插入数据库。

How to read text file data in sql server 2008???

I have a one text file and data insert into database.

推荐答案

您好,



您可以使用 BULK INSERT [ ^ ]。



例如

Hello,

You can use BULK INSERT[^] for this.

e.g.
BULK INSERT csv_test
FROM 'F:\csv_data.txt'
WITH
(
    FIELDTERMINATOR = ',',
    ROWTERMINATOR = '\n'
)
GO



上面的例子s将CSV文本文件中的数据插入到SQL服务器表中。



问候,


The above examples inserts data from a CSV text file into SQL server table.

Regards,


假设您的意思是读取文件在C#中将它插入到你的SQL Db中(否则为什么要在标签中提到C#?)然后你究竟是怎么做的取决于你在SQL中创建了什么类型的列。

如果它是VARCHAR:

Assuming that you mean to read the file in C# and insert it into your SQL Db (otherwise why mention C# in the tags?) then exactly how you do it depends on what kind of column you have created in SQL.
If it is VARCHAR:
string myFileString = File.ReadAllText(path);
using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand com = new SqlCommand("INSERT INTO myTable (myColumn) VALUES (@TEXT)", con))
        {
        com.Parameters.AddWithValue("@TEXT", myFileString);
        com.ExecuteNonQuery();
        }
    }

如果是VARBINARY:

If it is VARBINARY:

byte[] myFiledata = File.ReadAllBytes(path);
using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand com = new SqlCommand("INSERT INTO myTable (myColumn) VALUES (@DATA)", con))
        {
        com.Parameters.AddWithValue("@DATA", myFileData);
        com.ExecuteNonQuery();
        }
    }


这篇关于如何在SQL SERVER 2008中读取文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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