using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.IO;
namespace 导入数据
{
class Program
{
static void Main(
string[] args)
{
string str =
"Data Source=LENOVO-PC;Initial Catalog=MyDatabase;Integrated Security=True";
int n = -
1;
using (StreamReader sr=
new StreamReader(
"1.txt"))
{
string line = sr.ReadLine();
using (SqlConnection sco =
new SqlConnection(str))
{
string sql =
"insert into users1(logName, logPassWord) values(@logName,@logPassWord)";
SqlParameter[] ps = {
new SqlParameter(
"@logName",System.Data.SqlDbType.VarChar),
new SqlParameter(
"@logPassWord",System.Data.SqlDbType.VarChar)
};
sco.Open();
using (SqlCommand scd =
new SqlCommand(sql, sco))
{
scd.Parameters.AddRange(ps);
while ((line=sr.ReadLine())!=null)
{
string[] texts = line.Split(
new char[] {
','},StringSplitOptions.RemoveEmptyEntries);
ps[
0].Value = texts[
0];
ps[
1].Value = texts[
1];
n = scd.ExecuteNonQuery();
}
}
sco.Close();
}
}
if (n >
0)
{
Console.WriteLine(
"导入成功!");
}
else
{
Console.WriteLine(
"导入失败!");
}
Console.ReadKey();
}
}
}