首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

! C# datagridview 数据保存到SqlServer数据库…

2012-05-08 
紧急求助!! C# datagridview 数据保存到SqlServer数据库……我用 SqlConnection con dataBase.CreateConne

紧急求助!! 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;        } 

热点排行