标题:数据库操作的问题
只看楼主
yilan9076
Rank: 2
等 级:论坛游民
帖 子:9
专家分:10
注 册:2009-12-28
结帖率:100%
已结贴  问题点数:20 回复次数:1 
数据库操作的问题
怎么同时对两个数据表插入数据
搜索更多相关主题的帖子: 数据库 
2010-03-02 13:21
303770957
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:6
帖 子:838
专家分:2125
注 册:2005-9-10
得分:20 
你要是access数据库的话,分别写两个插入语句进行插入分开执行。比如:
#region //数据插入
if (option == "Insert")//是否为新行
            {
                try
                {
                    if (e.ColumnIndex == 1)//是否为名称列
                    {
                        if (!(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value == null))//非空
                        {
                                string sqlInsertMaterials = @"insert into Materials ( MaterialName) values ('";
                            sqlInsertMaterials += dataGridView1.CurrentCell.Value.ToString() + "')";

                            string sqlInsertNutrition = @"insert into Nutrition ( MaterialName) values ('";
                            sqlInsertNutrition += dataGridView1.CurrentCell.Value.ToString() + "')";

                            if (db.ExecuteSql(sqlInsertMaterials) == 1 && db.ExecuteSql(sqlInsertNutrition) == 1)
                            {
                                MessageBox.Show("插入成功!", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);

                            }
                        }
                    }
                }
                catch
                {
                    MessageBox.Show("插入失败!", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                finally { Bind();}
            }
#endregion

要是sql数据库写两个插入语句中间用分号隔开一次执行。比如:
#region //数据插入
if (option == "Insert")//是否为新行
            {
                try
                {
                    if (e.ColumnIndex == 1)//是否为名称列
                    {
                        if (!(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value == null))//非空
                        {
                               string sqlInsertMaterials = @"insert into Materials ( MaterialName) values ('";
                            sqlInsertMaterials += dataGridView1.CurrentCell.Value.ToString() + "')";

                            sqlInsertMaterials += @";insert into Nutrition ( MaterialName) values ('";
                            sqlInsertMaterials += dataGridView1.CurrentCell.Value.ToString() + "')";

                            if (db.ExecuteSql(sqlInsertMaterials) == 2)
                            {
                                MessageBox.Show("插入成功!", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);

                            }
                        }
                    }
                }
                catch
                {
                    MessageBox.Show("插入失败!", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                finally { Bind();}
            }
#endregion

因为access数据库不支持一次运行两个sql语句只能分别运行,sql数据库支持一次运行多个sql语句。

♂ 死后定当长眠,生前何须久睡。♀
2010-03-02 13:43



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-298381-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.124692 second(s), 7 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved