大众计算机学习网

专门提供各种电脑教程

Sql远程数据库与本地数据库的导入导出源代码—与虚拟主机黑店之斗智斗勇下篇

Admin | 2008-7-15 13:26:02 | 被阅次数 | 4957

        接上篇……

   //通过参数传递数据,将数据插入到本地数据库!
    private void DataInOut(string Title, string ColumnsID, string Detail, string DateTime, string Audit, string UserName,string ReadNum)
    {
        SqlConnection mycon = new SqlConnection("server=localhost;uid=sa;pwd='密码';database=数据库名");
        mycon.Open();
        SqlTransaction mytrans = mycon.BeginTransaction();
        // 为事务创建一个命令
        SqlCommand mycmd = new SqlCommand("insert into 数据表名(Title,ColumnsID,Detail,DateTime,Audit,UserName,ReadNum) values(@Title, @ColumnsID, @Detail, @DateTime, @Audit, @UserName,@ReadNum)", mycon);

        mycmd.Parameters.Add(new SqlParameter("@Title", SqlDbType.VarChar, 8000));
        mycmd.Parameters["@Title"].Value = Title;

        mycmd.Parameters.Add(new SqlParameter("@ColumnsID", SqlDbType.Int, 4));
        mycmd.Parameters["@ColumnsID"].Value = ColumnsID;

        mycmd.Parameters.Add(new SqlParameter("@Detail", SqlDbType.VarChar, 8000));
        mycmd.Parameters["@Detail"].Value = Detail;
        mycmd.Parameters.Add(new SqlParameter("@DateTime", SqlDbType.VarChar, 8000));
        mycmd.Parameters["@DateTime"].Value = DateTime;
        mycmd.Parameters.Add(new SqlParameter("@Audit", SqlDbType.VarChar, 8000));
        mycmd.Parameters["@Audit"].Value = Audit;
        mycmd.Parameters.Add(new SqlParameter("@UserName", SqlDbType.VarChar, 8000));
        mycmd.Parameters["@UserName"].Value = UserName;
        mycmd.Parameters.Add(new SqlParameter("@ReadNum", SqlDbType.Int, 4));
        mycmd.Parameters["@ReadNum"].Value = ReadNum;

        mycmd.Connection = mycon;
        mycmd.Transaction = mytrans;
        try
        {
            mycmd.ExecuteNonQuery();
            mytrans.Commit();
        }
        catch (Exception ex)
        {
            mytrans.Rollback();
            Response.Write(ex.ToString().Trim());
        }
        finally
        {
            mycon.Close();
        }
    }
}