如何将数据从Datagridview保存到SQL Server [英] How to save the data from datagridview to sql server

查看:170
本文介绍了如何将数据从Datagridview保存到SQL Server的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮助我,我只想单击提交按钮即可将数据从DataGridView保存到SqlServer.请尝试所有可能的方法,甚至在Google中搜索,但我没有得到答案.

我正在使用C#2008窗口应用程序和SqlServer 2005 Express


[edit]整理一下...-OriginalGriff [/edit]


HI


Please help me I just want to save the data from DataGridView to SqlServer on click of the submit button. Please I try all possible thing and even search in google but I am not getting the answer.

I am using C# 2008 window application and SqlServer 2005 Express


[edit]Just a tidy up... - OriginalGriff[/edit]


HI


<br />
I have completed half task the data is saving in sql but only one i want all data should save in sql 




(是否可以选择执行操作"或任何其他方式)


在此先感谢:rose :: rose :: rose:




(is there a option to do OR any other way)


Thanks in Advance :rose::rose::rose:

推荐答案




首先,您需要创建一个存储过程.

这样的东西
Hi,


First you need to create a stored procedure.

Something like this
For I = 0 To DataGridView.RowCount - 1
myCommand.Parameters.Add(@One;, Grid.items(i).value)
myCommand.Parameters.Add(@two;,  Grid.items(i).value) 
mycommand.executenonquery()
Next    




尝试一下.

在我的项目中效果很好.
它只能工作,因为我从不从任何其他数据集中设置datagrid数据源值.



问候,
萨兰.




Just try this.

It worked fine in my project.
It works only, at I never set the datagrid datasource value from any other Dataset.



regards,
saran.


使用 sqlbulkcopy 将网格数据保存到数据库中.
步骤:
1.将Griddata保存到数据表中
2.使用sqlbulkcopy插入数据

use sqlbulkcopy for save grid data into database.
Steps :
1. Save Griddata into datatable
2. Use sqlbulkcopy to insert data

public bool InserData(DataTable insertDt)
    {
// insertDt DataTable From UI
        using (SqlConnection objConn = new SqlConnection(objDBTrans.ConnectionString))
        {
            objConn.Open();
            using (SqlBulkCopy objBulkCopy = new SqlBulkCopy(objConn.ConnectionString, SqlBulkCopyOptions.FireTriggers))
            {
                objBulkCopy.DestinationTableName = "real_Propertytype";
               // Arvind W: Column Mapping with DataBase Table
                objBulkCopy.ColumnMappings.Add("Code", "code");

                objBulkCopy.WriteToServer(insertDt);
                objConn.Close();
                return true;
            }
        }
        return false;
    }


我希望这对您有帮助...:thumbsup:
Arvind Wanjari ....


I hope this is helpful for u ...:thumbsup:
Arvind Wanjari....


Aarti写道:

好吧,我已经从一个表中调用了数据,并想将数据保存到另一张表中,但是我需要更新该表中的一行,你能告诉我任何其他方法可以在此逻辑上工作吗.

我的回答:

当您只需要更新一行时,为什么要调用所有行并从前端再次写回所有行. (这只会增加机器的负载和时间).


否则,您可以通过仅从数据库中调用需要更新的那一行,然后将其写回到新表中来进行操作.在存储过程中,您只需编写一条命令即可将所有数据从一个表写到另一个表中表格仅留下一行,您可以从前端对其进行编辑和更新.这将降低复杂性并提高性能.
Aarti wrote:

well i have call the data from one table and want to save the data to another table but i need to update one row in that table can you tell me any other way where this logic will work.


My Answer:

When you need to update only one row, then why are you calling all the rows and writing back all the rows again from frontend. (This will only increase the load and time on the machine).


You can do it otherwise by calling only that row from the database, which needs to be updated, and then writing back it to a new table, In the stored procedure you can simply write a command to write all the data from one table to another table leaving one single row, which you have edited and updated from the front end.This will decrease complexity and increase performance also.


这篇关于如何将数据从Datagridview保存到SQL Server的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆