标题: 少了什么东西了乱码 [打印本页] 作者: 王国文 时间: 2012-5-23 03:08 标题: 少了什么东西了乱码 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;
namespace 导入
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public static string str = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True";
using (FileStream fileStream =File.OpenRead(ofdImport.FileName))
{
using (StreamReader stringreader = new StreamReader(fileStream))
{
string line = null;
while ((line = stringreader.ReadLine()) != null)
{
string[] strs = line.Split('|');
string name = strs[0];
int age = Convert.ToInt32(strs[1]);
using (SqlConnection con = new SqlConnection(str))
{
con.Open();
using (SqlCommand cmd =con.CreateCommand())
{
cmd.CommandText = "insert into T_persons (name,age) values (@name,@age)";
cmd.Parameters.Add(new SqlParameter("@name", name));
cmd.Parameters.Add(new SqlParameter("age",age));
cmd.ExecuteNonQuery();
}
}