怎么将dataGridView中的数据添加到SQL数据库中??

王先生,你好 2010-08-12 10:06:42
我要将dataGridView中的数据添加到SQL数据库中的表中,代码改怎么编写,我写了以下代码,不知道错在那里,请指教!

string connString = "Server=PC-201006011529;Trusted_Connection=SSPI;initial catalog=Test1;Connect Timeout=30";

SqlConnection conn = new SqlConnection(connString);

conn.Open();

string sqlCheckStr = "select * from MainSheet";

sa = new SqlDataAdapter(sqlCheckStr, conn);

dt = new DataTable();

dataGridView3.DataSource = dt;

bs = new BindingSource();

bs.DataSource = dt; //绑定BindingSource

sa.Update((DataTable)bs.DataSource);

conn.Close();
...全文
715 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
denbes 2010-08-16
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 peter200694013 的回复:]
eg:

C# code

using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlDataAdapter adapter = new SqlDataAdapter();
ada……
[/Quote]


这样去做就行了.
kj289907795 2010-08-16
  • 打赏
  • 举报
回复
这贴我也急需啊!!!!兄弟 关注你 帮顶
王先生,你好 2010-08-16
  • 打赏
  • 举报
回复
谁能给我一段经过测试的源代码吗??
王先生,你好 2010-08-16
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 yanlingoffice 的回复:]
string connString = "Server=PC-201006011529;Trusted_Connection=SSPI;initial catalog=Test1;Connect Timeout=30";

SqlConnection conn = new SqlConnection(connString);

conn.Open();

string sq……
[/Quote]


这样代码执行的结果是,把数据库中的内容显示到了dataGridView3中,结果是相反的
kj289907795 2010-08-16
  • 打赏
  • 举报
回复
后人来给段 调试过的代码吧。。。。
kj289907795 2010-08-16
  • 打赏
  • 举报
回复
我喜欢楼上,估计问题要解决了 省了我100分 HAHA
yanlingoffice 2010-08-16
  • 打赏
  • 举报
回复
string connString = "Server=PC-201006011529;Trusted_Connection=SSPI;initial catalog=Test1;Connect Timeout=30";

SqlConnection conn = new SqlConnection(connString);

conn.Open();

string sqlCheckStr = "select * from MainSheet";

sa = new SqlDataAdapter(sqlCheckStr, conn);

dt = new DataTable();


////这里需要对dt中的数据进行填充
da.Fill( dt );
////

dataGridView3.DataSource = dt;

bs = new BindingSource();

bs.DataSource = dt; //绑定BindingSource

sa.Update((DataTable)bs.DataSource);

conn.Close();
kj289907795 2010-08-16
  • 打赏
  • 举报
回复
这次再顶部上去 我就没法了 。。
andybang1981 2010-08-13
  • 打赏
  • 举报
回复
dt为空啊
wuyq11 2010-08-12
  • 打赏
  • 举报
回复
DataSet ds = new DataSet();
SqlDataAdapter sda;

SqlCommandBuilder scb = new SqlCommandBuilder(sda);
sda.Update(ds);
this.dataGridView1.DataSource = ds.Tables[0];
苦苦挣扎中 2010-08-12
  • 打赏
  • 举报
回复
不需要插入语句吗?
Peter200694013 2010-08-12
  • 打赏
  • 举报
回复
eg:

using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = new SqlCommand(queryString, connection);
SqlCommandBuilder builder = new SqlCommandBuilder(adapter);

connection.Open();

DataTable customers = new DataTable();
adapter.Fill(customers);

// code to modify data in DataTable here

adapter.Update(customers);

}
Peter200694013 2010-08-12
  • 打赏
  • 举报
回复
sa.Fill(dt);
allen3010 2010-08-12
  • 打赏
  • 举报
回复 1
非常好的问题
hytwl 2010-08-12
  • 打赏
  • 举报
回复
你那里是不是写错了 dt可以就是一起你绑定数据的dt啊?!
王先生,你好 2010-08-12
  • 打赏
  • 举报
回复
问题的关键是在这里,dt是空的。怎么把dataGridView中的数据放进到dt中
chazikai24 2010-08-12
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 peter200694013 的回复:]
sa.Fill(dt);
[/Quote]
你没发现你的dt是空的吗?
lelelehuhuhu 2010-08-12
  • 打赏
  • 举报
回复
实在不行就用INSERT插入吧。
Ryan20082009 2010-08-12
  • 打赏
  • 举报
回复
protected void But_insert_Click(object sender, EventArgs e)
{
string _isbn = this.Text_isbm.Text.Trim();
string _bookname = this.Text_bookname.Text.Trim();
string _price = this.Text_price.Text.Trim();
string _buydate = this.Text_buydate.Text.Trim();
string _desc = this.Text_descri.Text.Trim();
string _statues = this.Text_statues.Text.Trim();

if (_isbn == "")
{
Response.Write("<script language='javascript'>alert('图书编号不能为空!')</script>");
}
else if (_bookname == "")
{
Response.Write("<script language='javascript'>alert('图书名字不能为空!')</script>");
}
else if (_price == "")
{
Response.Write("<script language='javascript'>alert('图书价格不能为空!')</script>");
}
else if (_buydate == "")
{
Response.Write("<script language='javascript'>alert('图书购买日期不能为空!')</script>");
}
else if (_desc == "")
{
Response.Write("<script language='javascript'>alert('图书描述不能为空!')</script>");
}
else if (_statues == "")
{
Response.Write("<script language='javascript'>alert('图书状态不能为空!')</script>");
}
else
{
BookInformation books = new BookInformation();
books.ISBN = _isbn;
books.BookName = _bookname;
books.Price = _price;
books.BuyToday = _buydate;
books.Description = _desc;
books.Statues = _statues;

bool bo = basedao.GetDao().Add(books);
if (bo)
{
Response.Write("<script language='javascript'>alert('添加成功!');window.location='insertBookInformation.aspx';</script>");
}
else
{
Response.Write("<script language='javascript'>alert('添加失败!')</script>");
}
}
}
王先生,你好 2010-08-12
  • 打赏
  • 举报
回复
string connString = "Server=20100716-0819;Trusted_Connection=SSPI;initial catalog=Test1;Connect Timeout=30";

SqlConnection conn = new SqlConnection(connString);

conn.Open();

SqlDataAdapter adapter = new SqlDataAdapter();

string queryString = "select * from MainSheet";

adapter.SelectCommand = new SqlCommand(queryString, conn);

SqlCommandBuilder builder = new SqlCommandBuilder(adapter);

DataTable customers = new DataTable();

adapter.Fill(customers);

adapter.Update(customers);

根据 Peter200694013 提供的代码,改成上面的样子,还是不行啊??
请问是怎么回事??

110,565

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧