日期:2014-05-18  浏览次数:20978 次

紧急求助!! C# datagridview 数据保存到SqlServer数据库……
我用 SqlConnection con = dataBase.CreateConnection(); 连接数据库,dataBase.CreateConnection是我定义的一个连接数据库的类。
现在,我要把datagridview表中的内容保存到SQLServer数据库中,保存完以后重新显示到datagridview中。
请高手帮忙,这个应该怎么实现,最好能附上代码。
谢谢 ~

------解决方案--------------------
就是一个update,一个重新绑定

C# code

SqlConnection conn = new SqlConnection(connString);
cmd = conn.CreateCommand();
cmd.CommandText = "select * from Test_Table";
SqlDataAdapter da = new SqlDataAdapter(cmd);
SqlCommandBuilder cb = new SqlCommandBuilder(da);
da.Update(ds);

------解决方案--------------------
给你俩方法吧,看不懂就真没办法了!
C# code

public void OpenSqlConnection()
        {
            filePath = "Data Source=172.24.20.5;Persist Security Info=True;User ID=sa;Password=fc.erp;Initial Catalog=GDIS";
            constr = filePath;
            if (sqlConn.State != ConnectionState.Open)
            {
                sqlConn.Close();
                sqlConn.ConnectionString = constr;
                myCmd.Connection = sqlConn;
                try
                {
                    sqlConn.Open();
                }
                catch (Exception e)
                {
                    throw new Exception(e.Message);
                }
            }
        }
public int UpData(DataSet MyDs)
        {
            int iReturn = 0;
            try
            {
                OpenSqlConnection();
                for (int i = 0; i < MyDs.Tables.Count; i++)
                {
                    DataTable dt = MyDs.Tables[i];
                    if (dt != null)
                    {
                        SqlDataAdapter da = GetDataAdapter("SELECT * FROM " + dt.TableName);
                        iReturn = da.Update(dt);
                    }
                    else
                        throw new Exception();
                }
            }
            catch (Exception ex)
            {
                try
                {
                    CloseSqlConnection();
                }
                catch (Exception err)
                {
                    throw new Exception(err.Message);
                }
                throw ex;
            }
            finally
            {
                CloseSqlConnection();
            }
            return iReturn;
        }

------解决方案--------------------
C# code

public SqlDataAdapter GetDataAdapter(string strSQL)
        {
            SqlDataAdapter da = null;
            try
            {
                da = new SqlDataAdapter();
                da.SelectCommand = new SqlCommand(strSQL, sqlConn);
                SqlCommandBuilder custCB = new SqlCommandBuilder(da);
                if (sqlConn != null)
                {
                    if (da.SelectCommand != null)
                        da.SelectCommand.Transaction = trans;
                    if (da.DeleteCommand != null)
                        da.DeleteCommand.Transaction = trans;
                    if (da.InsertCommand != null)
                        da.InsertCommand.Transaction = trans;
                    if (da.UpdateCommand != null)
                        da.UpdateCommand.Transaction = trans;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return da;
        }