我照着视频上写了一个导入文件的程序 为什么报Invalid object name 'T_Person'.异常?
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
using (FileStream fs = File.OpenRead(openFileDialog1.FileName))
{
using (StreamReader sr=new StreamReader(fs))
{
string line = null;
while ((line = sr.ReadLine()) != null)
{
string[] data = line.Split('|');
string name = data[0];
int age = Convert.ToInt32(data[1]);
string connectionString =
"Data Source = (LocalDB)\\v11.0;AttachDbFilename=\"C:\\Users\\Olive.C\\documents\\visual studio 2012\\Projects\\MyProject\\Demo\\Database1.mdf\";Integrated Security=True";
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
using (SqlCommand command = conn.CreateCommand())
{
command.CommandText = "insert into T_Person (name,age)values(@name,@age)";
command.Parameters.Add(new SqlParameter("name", name));
command.Parameters.Add(new SqlParameter("age", age));
command.ExecuteNonQuery();
}
}
}
}
}
}
}
}
}