C# 将单元格值从GridView导出到SQL表的基本方法
我需要读一门关于如何将DevExpress中的gridView导出到SQL表的小课程。。 像表Employee和i的First Name、Father Name、Last Name等值一样,我有很多行。。 如何循环到每一行并将数据发送到数据库 提前Thx 我尝试了以下代码:C# 将单元格值从GridView导出到SQL表的基本方法,c#,sql,database,devexpress,C#,Sql,Database,Devexpress,我需要读一门关于如何将DevExpress中的gridView导出到SQL表的小课程。。 像表Employee和i的First Name、Father Name、Last Name等值一样,我有很多行。。 如何循环到每一行并将数据发送到数据库 提前Thx 我尝试了以下代码: string sql = @"INSERT INTO Emp (@FName, @MName,@LName, @Code, @TaxNb, @SSN, @EmploymentType, @DOB, @MarStat, @Re
string sql = @"INSERT INTO Emp (@FName, @MName,@LName, @Code, @TaxNb, @SSN, @EmploymentType, @DOB, @MarStat, @RegNum, @BadgeNum, @HireDate, @TaxSince, @HireSince, @ArEmpName, @ArFatherName, @ArLastName, ArPayUnit)";
DataTable table = new DataTable();
try
{
SqlConnection connection = new SqlConnection(@"workstation id = PC-PC; user id=sa;Password=sapassword; data source=pc-pc; persist security info=True;initial catalog=CleanPayrollTest2");
SqlCommand command = new SqlCommand(sql, connection);
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.InsertCommand = command;
connection.Open();
// for (int i =0; i< gridView3.RowCount; i++)
//{
//command.Parameters.Add(@FirstName, gridView3.GetRowCellValue(i,gridView3.Columns));
//adapter.InsertCommand.ExecuteNonQuery();
//}
SqlParameter[] MyParams = new SqlParameter[28];
MyParams[0] = new SqlParameter("@FName", SqlDbType.VarChar, 20);
MyParams[0].SourceColumn = FirstName;
command.Parameters.Add("@FName", SqlDbType.VarChar, 20);
MyParams[1] = new SqlParameter("@MName", SqlDbType.VarChar, 20);
MyParams[1].SourceColumn = FatherName;
MyParams[2] = new SqlParameter("@LName", SqlDbType.VarChar, 20);
MyParams[2].SourceColumn = LastName;
string sql=@“插入Emp(@FName、@MName、@LName、@code、@TaxNb、@SSN、@EmploymentType、@DOB、@MarStat、@RegNum、@BadgeNum、@HireDate、@TaxSince、@ArEmpName、@ArFatherName、@ArLastName、ArPayUnit)”;
DataTable=新的DataTable();
尝试
{
SqlConnection connection=new SqlConnection(@“工作站id=PC-PC;用户id=sa;密码=sapassword;数据源=PC PC;持久安全信息=True;初始目录=CleanPayrollTest2”);
SqlCommand=新的SqlCommand(sql,连接);
SqlDataAdapter=新的SqlDataAdapter();
adapter.InsertCommand=命令;
connection.Open();
//对于(int i=0;i
来自:
SqlDataAdapter充当数据集和SQL之间的桥梁
用于检索和保存数据的服务器
在您描述的场景中,不需要“桥接”。只需使用SqlCommand
,将SqlParameter
的集合添加到其中,然后调用ExecuteNonQuery()
执行插入
using(SqlConnection connection = new SqlConnection(@"workstation id = PC-PC; user id=sa;Password=sapassword; data source=pc-pc; persist security info=True;initial catalog=CleanPayrollTest2"))
{
using(SqlCommand command = new SqlCommand(sql, connection))
{
try
{
connection.Open();
for (int i =0; i< gridView3.RowCount; i++)
{
SqlParameter parameter = new SqlParameter();
// TODO: handle name accordingly (MName, LName etc.)
parameter.ParameterName = "@FName";
// TODO: handle type accordingly
parameter.SqlDbType = SqlDbType.NVarChar;
parameter.Direction = ParameterDirection.Input;
// TODO: use the field name accordingly
parameter.Value = Convert.ToString(gridView3.GetRowCellValue(i, "FieldName"));
// add the parameter to the command
command.Parameters.Add(parameter);
}
command.ExecuteNonQuery();
}
catch(Exception)
{
// TODO: handle the exception
}
}
}
使用(SqlConnection connection=newsqlconnection(@“工作站id=PC-PC;用户id=sa;密码=sapassword;数据源=PC PC;持久安全信息=True;初始目录=CleanPayrollTest2”))
{
使用(SqlCommand=newsqlcommand(sql,连接))
{
尝试
{
connection.Open();
对于(int i=0;i
备注:您应该在代码中处理SQL相关对象-一种方便的方法是使用语句。来自:
SqlDataAdapter充当数据集和SQL之间的桥梁
用于检索和保存数据的服务器
在您描述的场景中,不需要“桥接”。只需使用SqlCommand
,将SqlParameter
的集合添加到其中,然后调用ExecuteNonQuery()
执行插入
using(SqlConnection connection = new SqlConnection(@"workstation id = PC-PC; user id=sa;Password=sapassword; data source=pc-pc; persist security info=True;initial catalog=CleanPayrollTest2"))
{
using(SqlCommand command = new SqlCommand(sql, connection))
{
try
{
connection.Open();
for (int i =0; i< gridView3.RowCount; i++)
{
SqlParameter parameter = new SqlParameter();
// TODO: handle name accordingly (MName, LName etc.)
parameter.ParameterName = "@FName";
// TODO: handle type accordingly
parameter.SqlDbType = SqlDbType.NVarChar;
parameter.Direction = ParameterDirection.Input;
// TODO: use the field name accordingly
parameter.Value = Convert.ToString(gridView3.GetRowCellValue(i, "FieldName"));
// add the parameter to the command
command.Parameters.Add(parameter);
}
command.ExecuteNonQuery();
}
catch(Exception)
{
// TODO: handle the exception
}
}
}
使用(SqlConnection connection=newsqlconnection(@“工作站id=PC-PC;用户id=sa;密码=sapassword;数据源=PC PC;持久安全信息=True;初始目录=CleanPayrollTest2”))
{
使用(SqlCommand=newsqlcommand(sql,连接))
{
尝试
{
connection.Open();
对于(int i=0;i
备注:您应该在代码中处理SQL相关对象-一种方便的方法是使用语句。我编辑了我的答案,向您展示了我的尝试。.该代码没有给我任何错误。.但是我在数据库中没有得到任何数据。为什么不在询问之前使用Google?下面是一篇文章()这很好地描述了这个过程。研究一下使用Linq和存储过程,这是一种更好、更干净的方法。为什么需要使用
SqlDataAdapter
?只需使用命令。ExecuteNonQuery()
来执行插入。也许你应该使用几个语句…我编辑了我的答案,向你展示了我的尝试..代码没有给我任何错误..但是我在数据库中没有得到任何数据为什么你不在提问之前使用谷歌?这里有一篇文章()这很好地描述了这个过程。研究一下使用Linq和存储过程,这是一种更好、更干净的方法。为什么需要使用SqlDataAdapter
?只需使用命令。ExecuteNonQuery()
执行插入。您可能还应该使用一些语句…非常感谢..这真的很有帮助我收到了以下错误:“必须