各位论坛的大哥大姐们,有个问题请教:
我用C#做了个简单的信息管理系统,就是MIS,其中有一个功能是批量导入数据,现在我能把数据导入到窗口上的一个datagridview里面,但是怎样实现将所有的数据
导入数据库里呢?我打算使用dataset,但是老出错,各位有什么好的主意吗?最好能给点代码,急啊,明天要交工了!!先谢谢了

解决方案 »

  1.   

    没看你的代码,不知道具体问题
    不过可以把dataset里的数据分解出来
    放到存储过程里或者拼接出来的sql语句里
    插入到数据库中 
      

  2.   

    看搂主挺急,那么不如简单一点:
    循环DataGridView里面的每一行,然后插入到数据库
    for (int index = 0; index < this.dataGridView1.Rows.Count; index++)
    {
       先提取数据记录,然后插入到数据库.....
    }这样搂主也比较好理解,还可以多现程操作,显示一个进度条,很方便
    并且出错后,还可以判断到底哪条数据入库出错。
      

  3.   

    用DATASET的writexml和readxml可以直接实现导入导出。
      

  4.   

    这是从MSDN摘来的,是用于sqlserver2005mobile的,不知道你能不能借鉴。
    获取或设置用于将新记录插入到数据源中的 SQL 语句。 命名空间:System.Data.SqlServerCe
    程序集:System.Data.SqlServerCe(在 system.data.sqlserverce.dll 中)语法
    Visual Basic(声明) 
    Public Property InsertCommand As SqlCeCommand
     
    Visual Basic(用法) 
    Dim instance As SqlCeDataAdapter
    Dim value As SqlCeCommandvalue = instance.InsertCommandinstance.InsertCommand = value
     
    C# 
    public SqlCeCommand InsertCommand { get; set; }
     
    C++ 
    public:
    property SqlCeCommand^ InsertCommand {
    SqlCeCommand^ get ();
    void set (SqlCeCommand^ value);
    }
     
    J# 
    /** @property */
    public SqlCeCommand get_InsertCommand ()/** @property */
    public void set_InsertCommand (SqlCeCommand value) 
    JScript 
    public function get InsertCommand () : SqlCeCommandpublic function set InsertCommand (value : SqlCeCommand) 属性值
    在 Update 过程中使用的 SqlCeCommand,用于将与 DataSet 中的新行相对应的记录插入到数据源中。 
    备注
    在调用 Update 过程中,如果未设置此属性而且 DataSet 中存在主键信息,那么在设置 SelectCommand 属性并使用 SqlCeCommandBuilder 的情况下,可以自动生成 InsertCommand。然后,SqlCeCommandBuilder 将生成其他任何未设置的命令。此生成逻辑要求 DataSet 中存在键列信息。在将 InsertCommand 分配给以前创建的 SqlCeCommand 时,不克隆 SqlCeCommand。InsertCommand 维护对以前创建的 SqlCeCommand 对象的引用。注意 
    如果执行此命令后返回行,这些行可能会添加到 DataSet 中,具体取决于如何设置 SqlCeCommand 对象的 UpdatedRowSource 属性。
     示例
    下面的示例创建一个 SqlCeDataAdapter 并设置它的一些属性。Visual Basic  复制代码 
    Dim cmd As SqlCeCommand = Nothing
    Dim adp As SqlCeDataAdapter = NothingTry
        adp = New SqlCeDataAdapter()
        Dim conn As New SqlCeConnection("Data Source = MyDatabase.sdf")    ' Create the SelectCommand
        '
        cmd = conn.CreateCommand()
        cmd.CommandText = "SELECT [Employee ID], [First Name], [Last Name] FROM Employees"
        adp.SelectCommand = cmd    ' Create the InsertCommand
        '
        cmd = conn.CreateCommand()
        cmd.CommandText = "INSERT INTO Employees ([First Name],[Last Name]) VALUES (@first, @last)"    Dim p As SqlCeParameter = Nothing    p = cmd.Parameters.Add("@first", SqlDbType.NVarChar, 10, "First Name")
        p.SourceVersion = DataRowVersion.Original    p = cmd.Parameters.Add("@last", SqlDbType.NVarChar, 20, "Last Name")
        p.SourceVersion = DataRowVersion.Original    adp.InsertCommand = cmd    ' Create the UpdateCommand
        '
        cmd = conn.CreateCommand()
        cmd.CommandText = "UPDATE Employees SET [First Name] = @first, " + _
            "[Last Name] = @last WHERE [Employee ID] = @employeeID"    p = cmd.Parameters.Add("@first", SqlDbType.NVarChar, 10, "First Name")
        p.SourceVersion = DataRowVersion.Current    p = cmd.Parameters.Add("@last", SqlDbType.NVarChar, 20, "Last Name")
        p.SourceVersion = DataRowVersion.Current    p = cmd.Parameters.Add("@employeeID", SqlDbType.NVarChar, 20, "Employee ID")
        p.SourceVersion = DataRowVersion.Original    adp.UpdateCommand = cmd    ' Populate the dataset with the results from the SELECT statement
        '
        Dim ds As New DataSet()
        adp.Fill(ds)    ' Modify the dataset
        '
        MessageBox.Show("Number of rows: " & ds.Tables(0).Rows.Count)    ' Insert some rows
        '
        ds.Tables(0).Rows.Add(New Object() {Nothing, "Nancy", "Smith"})
        ds.Tables(0).Rows.Add(New Object() {Nothing, "Joe", "Clayton"})    ' Update some rows
        '
        ds.Tables(0).Rows(1)(1) = "David"
        ds.Tables(0).Rows(1)(2) = "Johnson"    ' This will execute two INSERT and one UPDATE statements
        '
        adp.Update(ds.Tables(0))
    Catch e As Exception
        MessageBox.Show(e.Message)
    Finally
        If Not Nothing Is adp.SelectCommand Then
            adp.SelectCommand.Dispose()
        End If
        If Not Nothing Is adp.InsertCommand Then
            adp.InsertCommand.Dispose()
        End If
    End Try 
    C#  复制代码 
    SqlCeCommand cmd = null;
    SqlCeDataAdapter adp = null;try
    {
        adp = new SqlCeDataAdapter();
        SqlCeConnection conn = new SqlCeConnection("Data Source = MyDatabase.sdf");    // Create the SelectCommand
        //
        cmd = conn.CreateCommand();
        cmd.CommandText = "SELECT [Employee ID], [First Name], [Last Name] FROM Employees";
        adp.SelectCommand = cmd;    // Create the InsertCommand
        //
        cmd = conn.CreateCommand();
        cmd.CommandText = "INSERT INTO Employees ([First Name],[Last Name]) VALUES (@first, @last)";    SqlCeParameter p = null;    p = cmd.Parameters.Add("@first", SqlDbType.NVarChar, 10, "First Name");
        p.SourceVersion = DataRowVersion.Original;    p = cmd.Parameters.Add("@last", SqlDbType.NVarChar, 20, "Last Name");
        p.SourceVersion = DataRowVersion.Original;    adp.InsertCommand = cmd;    // Create the UpdateCommand
        //
        cmd = conn.CreateCommand();
        cmd.CommandText = "UPDATE Employees SET [First Name] = @first, " +
            "[Last Name] = @last WHERE [Employee ID] = @employeeID";    p = cmd.Parameters.Add("@first", SqlDbType.NVarChar, 10, "First Name");
        p.SourceVersion = DataRowVersion.Current;    p = cmd.Parameters.Add("@last", SqlDbType.NVarChar, 20, "Last Name");
        p.SourceVersion = DataRowVersion.Current;    p = cmd.Parameters.Add("@employeeID", SqlDbType.NVarChar, 20, "Employee ID");
        p.SourceVersion = DataRowVersion.Original;    adp.UpdateCommand = cmd;    // Populate the dataset with the results from the SELECT statement
        //
        DataSet ds = new DataSet();
        adp.Fill(ds);    // Modify the dataset
        //
        MessageBox.Show("Number of rows: " + ds.Tables[0].Rows.Count);    // Insert some rows
        //
        ds.Tables[0].Rows.Add(new object[] { null, "Nancy", "Smith" });
        ds.Tables[0].Rows.Add(new object[] { null, "Joe", "Clayton" });    // Update some rows
        //
        ds.Tables[0].Rows[1][1] = "David";
        ds.Tables[0].Rows[1][2] = "Johnson";    // This will execute two INSERT and one UPDATE statements 
        //
        adp.Update(ds.Tables[0]);    
    }
    catch (Exception e)
    {
        MessageBox.Show(e.Message);
    }
    finally
    {
        if (null != adp.SelectCommand) adp.SelectCommand.Dispose();
        if (null != adp.InsertCommand) adp.InsertCommand.Dispose();

      

  5.   

    问题应该很简单才对呀,,你既然能把数据导到GV了.再把GV里的数据保存到数据库是很简单的呀,FOR()行保存就行了.只是对应的问题要调好就可以了,.,