SqlDataAdaptert和添加数据
发表在C#图书答疑
2009-05-26
是否精华
是
否
版块置顶:
是
否
using System.Data.SqlClient;
//SqlDataAdapter使用
SqlConnection con = new SqlConnection();
con.ConnectionString = "Server=.;uid=sa;pwd=sa;database=db_PMS";
con.Open();
DataTable dt = new DataTable("Resouce");
string SqlIns = "insert into tb_user values('" + textBox1.Text + " ','" + textBox2.Text + "')";
SqlCommand command = new SqlCommand(SqlIns, con);
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = command;
adapter.Fill(dt);
dt.Clear();
SqlDataAdapter AdapterSelect = new SqlDataAdapter("select * from tb_User", con);
AdapterSelect.Fill(dt);
dataGridView1.DataSource = dt.DefaultView;
我运行的时候,提示adapter.Fill(dt);出现异常 , DataTable dt = new DataTable("Resouce");这条语句什么意思不明白。Resouce 是什么意思?
//添加
using System.Data.SqlClient;
try
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Server=.;uid=sa;pwd=sa;database=db_PMS";
conn.Open();
string SqlIns = "insert into tb_department values('" + textBox1.Text + "','" + textBox2.Text + "')";
SqlCommand command = new SqlCommand();
command.CommandText = SqlIns;
command.Connection = conn;
command.ExecuteNonQuery();
MessageBox.Show("数据库添加成功!");
SqlDataAdapter AdapterSelect = new SqlDataAdapter("select * from tb_department", conn);
DataTable dt = new DataTable();
AdapterSelect.Fill(dt);
dataGridView1.DataSource = dt.DefaultView;
}
catch (Exception ee)
{
MessageBox.Show(ee.Message.ToString());
}
运行没有反应
//SqlDataAdapter使用
SqlConnection con = new SqlConnection();
con.ConnectionString = "Server=.;uid=sa;pwd=sa;database=db_PMS";
con.Open();
DataTable dt = new DataTable("Resouce");
string SqlIns = "insert into tb_user values('" + textBox1.Text + " ','" + textBox2.Text + "')";
SqlCommand command = new SqlCommand(SqlIns, con);
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = command;
adapter.Fill(dt);
dt.Clear();
SqlDataAdapter AdapterSelect = new SqlDataAdapter("select * from tb_User", con);
AdapterSelect.Fill(dt);
dataGridView1.DataSource = dt.DefaultView;
我运行的时候,提示adapter.Fill(dt);出现异常 , DataTable dt = new DataTable("Resouce");这条语句什么意思不明白。Resouce 是什么意思?
//添加
using System.Data.SqlClient;
try
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Server=.;uid=sa;pwd=sa;database=db_PMS";
conn.Open();
string SqlIns = "insert into tb_department values('" + textBox1.Text + "','" + textBox2.Text + "')";
SqlCommand command = new SqlCommand();
command.CommandText = SqlIns;
command.Connection = conn;
command.ExecuteNonQuery();
MessageBox.Show("数据库添加成功!");
SqlDataAdapter AdapterSelect = new SqlDataAdapter("select * from tb_department", conn);
DataTable dt = new DataTable();
AdapterSelect.Fill(dt);
dataGridView1.DataSource = dt.DefaultView;
}
catch (Exception ee)
{
MessageBox.Show(ee.Message.ToString());
}
运行没有反应