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

更新datatable的问题,急~~~~~~~~~~~~~~~~~~~~~~~
C# code

public Boolean UpdateDataTable3(DataTable dt, string strTable)
        {
            SqlDataAdapter da = new SqlDataAdapter();
            SqlConnection conn = commSql.ReturnDbConn();

            string a1 = "";
            string a2 = "";
            string a3 = "";

            for (int i = 0; i < dt.Columns.Count; i++)
            {
                if (dt.Columns[i].ColumnName.ToString().ToUpper() != "ID")
                {
                    a1 = a1 + dt.Columns[i].ColumnName.ToString() + ",";
                    a2 = a2 + "@" + dt.Columns[i].ColumnName.ToString() + ",";
                    a3 = a3 + dt.Columns[i].ColumnName.ToString() + "=@" + dt.Columns[i].ColumnName.ToString() + ",";
                }
            }

            a1 = a1.Substring(0, a1.Length - 1);
            a2 = a2.Substring(0, a2.Length - 1);
            a3 = a3.Substring(0, a3.Length - 1);

            da.InsertCommand = new SqlCommand("Insert Into " + strTable + "(" + a1 + ") Values(" + a2 + ")", conn);
            da.InsertCommand.CommandType = CommandType.Text;

            da.UpdateCommand = new SqlCommand("Update " + strTable + " Set " + a3 + " Where ID=@ID", conn);
            da.UpdateCommand.CommandType = CommandType.Text;

            da.DeleteCommand = new SqlCommand("Delete From " + strTable + " Where ID=@ID", conn);
            da.DeleteCommand.CommandType = CommandType.Text;


            for (int i = 0; i < dt.Columns.Count; i++)
            {

                da.InsertCommand.Parameters.Add("@" + dt.Columns[i].ColumnName.ToString(), ReturnDbType(dt.Columns[i].DataType.ToString()), dt.Columns[i].MaxLength, dt.Columns[i].ColumnName);

                da.UpdateCommand.Parameters.Add("@" + dt.Columns[i].ColumnName.ToString(), ReturnDbType(dt.Columns[i].DataType.ToString()), dt.Columns[i].MaxLength, dt.Columns[i].ColumnName);
                da.DeleteCommand.Parameters.Add("@" + dt.Columns[i].ColumnName.ToString(), ReturnDbType(dt.Columns[i].DataType.ToString()), dt.Columns[i].MaxLength, dt.Columns[i].ColumnName);

                
            }
            da.DeleteCommand.Parameters.Add("@id", SqlDbType.Int, 4, "id");

            try
            {
                da.Update(dt );
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
 

        }

        SqlDbType ReturnDbType(string strTypeName)
        {
            switch (strTypeName)
            {
                case "System.String":
                    return SqlDbType.NVarChar;
                    break;

                case "System.Int32":
                    return SqlDbType.Int;
                    break;

                case "System.DateTime":
                    return SqlDbType.DateTime;
                    break;

                case "System.Decimal":
                    return SqlDbType.Decimal;
                    break;

                case "System.Byte":
                    return SqlDbType.Image;
                    break;

                case "System.Boolean":
                    return SqlDbType.Bit;
                    break;

                case "System.Double":
                    return SqlDbType.Float;
                    break;

                case "System.Single":
                    return SqlDbType.Real;
                    break;

                default:
                    return SqlDbType.NVarChar;
                    break;
            }
        }



我想要实现