CSV - 通过代码将文件导入 Sql 数据库
CSV - Import file to a Sql database through code
我需要将文本 file/csv 文件中的数据加载到 SQL 服务器数据库。我使用下面显示的代码加载数据并加载到数据库问题是第二列中的数据可能包含 space 但我使用 space 分隔列数据。
即
200007 XXXX Check XXXX yyy 50
200013 YYYY Check ZZZZ yyy 50
200022 nnnn 25Mg 30 Tabs
200042 mmmm 30 Mg 30 Tabs
我需要在第一列中存储第一个 ID 号,在第二列中存储其余文本:
string str = Properties.Settings.Default.con;
SqlConnection con = new SqlConnection(str);
SqlCommand cmd = new SqlCommand();
try
{
cmd.Connection = con;
con.Open();
cmd.CommandText = "IF NOT EXISTS (SELECT * FROM sysobjects WHERE name='TEMP_AUTO' AND xtype='U')" +
"CREATE TABLE TEMP_AUTO (" +
"ID varChar(10) NULL," +
"NAME varChar(50) NULL," +
"DATE TIMESTAMP NULL," +
")";
cmd.ExecuteNonQuery();
string query1 = "INSERT INTO [dbo].[TEMP_AUTO]([ID],[NAME]) VALUES (@num1, @num2)";
cmd.CommandText = query1;
string[] allLines = File.ReadAllLines(txtFilePath.Text);
for (int i = 0; i < allLines.Length; i++)
{
cmd.Parameters.Clear();
string[] items = allLines[i].Split(new char[] { ' ' });
cmd.Parameters.AddWithValue("@num1", items[0]);
cmd.Parameters.AddWithValue("@num2", items[1]);
cmd.ExecuteNonQuery();
}
MessageBox.Show("Successfully saved your data");
}
finally
{
cmd.Dispose();
con.Close();
}
使用 SSIS 将此文件映射到 SQL Table.
,只要它具有标准结构即可
这是一次性的吗?您是否尝试过在 Excel 中组织数据,然后使用 SSMS 导入工具将其导入?如果您右键单击数据库,然后单击任务 > 导入数据,当给出选择源的选项时,向导将出现,选择 Excel。平面文件是一个选项,但如果您可以先将其格式化为 Excel,则效果会更好。
点击浏览时,您可以调整列类型和分隔符的位置,就像导入 Excel 本身一样。
使用 String.Split(Char[], Int32) 方法仅在第一次出现 ' ' 时拆分。例如
string[] items = allLines[i].Split(new char[] { ' ' }, 2);
参考文献:MSDN and previous relevant question
一个可能的解决方案可能是这样的:
string[] allLines = {
"200007 XXXX Check XXXX yyy 50",
"200013 YYYY Check ZZZZ yyy 50",
"200015 ",
"2541111"
};
for (int i = 0; i < allLines.Length; i++)
{
string param1 = null;
string param2 = null;
int spaceIndex = allLines[i].IndexOf(' ');
if (spaceIndex > 0)
{
param1 = allLines[i].Substring(0, spaceIndex);
if (spaceIndex < allLines[i].Length - 1)
{
param2 = allLines[i].Substring(spaceIndex + 1, allLines[i].Length-1 - spaceIndex);
}
}
else
{
param1 = allLines[i];
}
Console.WriteLine("param1:{0} param2:{1}", param1, param2);
}
使用下面的代码。
using (StreamReader sr = File.OpenText("txtFile.txt")) // Mention the path,if the file is not in application folder.
{
string str = String.Empty;<br/>
while ((str = sr.ReadLine()) != null)
{
string[] item = str.Split(' ');
SqlConnection con = new SqlConnection(str);
SqlCommand cmd = new SqlCommand();
string query1 = "INSERT INTO [dbo].[TEMP_AUTO]([ID],[NAME]) VALUES ('" + item[0] + "', '" + item[1] + "')";
// Do remain part<br/>
}
}
我需要将文本 file/csv 文件中的数据加载到 SQL 服务器数据库。我使用下面显示的代码加载数据并加载到数据库问题是第二列中的数据可能包含 space 但我使用 space 分隔列数据。
即
200007 XXXX Check XXXX yyy 50
200013 YYYY Check ZZZZ yyy 50
200022 nnnn 25Mg 30 Tabs
200042 mmmm 30 Mg 30 Tabs
我需要在第一列中存储第一个 ID 号,在第二列中存储其余文本:
string str = Properties.Settings.Default.con;
SqlConnection con = new SqlConnection(str);
SqlCommand cmd = new SqlCommand();
try
{
cmd.Connection = con;
con.Open();
cmd.CommandText = "IF NOT EXISTS (SELECT * FROM sysobjects WHERE name='TEMP_AUTO' AND xtype='U')" +
"CREATE TABLE TEMP_AUTO (" +
"ID varChar(10) NULL," +
"NAME varChar(50) NULL," +
"DATE TIMESTAMP NULL," +
")";
cmd.ExecuteNonQuery();
string query1 = "INSERT INTO [dbo].[TEMP_AUTO]([ID],[NAME]) VALUES (@num1, @num2)";
cmd.CommandText = query1;
string[] allLines = File.ReadAllLines(txtFilePath.Text);
for (int i = 0; i < allLines.Length; i++)
{
cmd.Parameters.Clear();
string[] items = allLines[i].Split(new char[] { ' ' });
cmd.Parameters.AddWithValue("@num1", items[0]);
cmd.Parameters.AddWithValue("@num2", items[1]);
cmd.ExecuteNonQuery();
}
MessageBox.Show("Successfully saved your data");
}
finally
{
cmd.Dispose();
con.Close();
}
使用 SSIS 将此文件映射到 SQL Table.
,只要它具有标准结构即可这是一次性的吗?您是否尝试过在 Excel 中组织数据,然后使用 SSMS 导入工具将其导入?如果您右键单击数据库,然后单击任务 > 导入数据,当给出选择源的选项时,向导将出现,选择 Excel。平面文件是一个选项,但如果您可以先将其格式化为 Excel,则效果会更好。
点击浏览时,您可以调整列类型和分隔符的位置,就像导入 Excel 本身一样。
使用 String.Split(Char[], Int32) 方法仅在第一次出现 ' ' 时拆分。例如
string[] items = allLines[i].Split(new char[] { ' ' }, 2);
参考文献:MSDN and previous relevant question
一个可能的解决方案可能是这样的:
string[] allLines = {
"200007 XXXX Check XXXX yyy 50",
"200013 YYYY Check ZZZZ yyy 50",
"200015 ",
"2541111"
};
for (int i = 0; i < allLines.Length; i++)
{
string param1 = null;
string param2 = null;
int spaceIndex = allLines[i].IndexOf(' ');
if (spaceIndex > 0)
{
param1 = allLines[i].Substring(0, spaceIndex);
if (spaceIndex < allLines[i].Length - 1)
{
param2 = allLines[i].Substring(spaceIndex + 1, allLines[i].Length-1 - spaceIndex);
}
}
else
{
param1 = allLines[i];
}
Console.WriteLine("param1:{0} param2:{1}", param1, param2);
}
使用下面的代码。
using (StreamReader sr = File.OpenText("txtFile.txt")) // Mention the path,if the file is not in application folder.
{
string str = String.Empty;<br/>
while ((str = sr.ReadLine()) != null)
{
string[] item = str.Split(' ');
SqlConnection con = new SqlConnection(str);
SqlCommand cmd = new SqlCommand();
string query1 = "INSERT INTO [dbo].[TEMP_AUTO]([ID],[NAME]) VALUES ('" + item[0] + "', '" + item[1] + "')";
// Do remain part<br/>
}
}