如果需要转移SQL Server数据你想到的是什么?大多基于MS平台开发的朋友大多第一时间会想到MSSQL的数据库备份还原或者导入导出向导。
其实如果数据量不多, 我们还可以这么玩——自己花二分钟写个工具来处理数据。
第一:我们可以摆脱那些笨重的工具。
第二:煅炼自己的动手能力。
第三:简单方便易行。
有这么多优点,我们为什么要说不呢?
原理,使用ADO.NET读取数据,然后遍历生成INSERT 语句。代码如下:
<%@ Page Language="C#" %> <%@ Import Namespace="System" %> <%@ Import Namespace="System.Collections" %> <%@ Import Namespace="System.Data" %> <%@ Import Namespace="System.Web" %> <%@ Import Namespace="System.Web.UI" %> <%@ Import Namespace="System.Web.UI.HtmlControls" %> <%@ Import Namespace="System.Web.UI.WebControls" %> <%@ Import Namespace="System.Data.SqlClient" %> <%@ Import Namespace="System.Management" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> protected void btnBegin_Click(object sender, EventArgs e) { string conn = txtConnection.Value; DataTable dt = ExcuteTable(conn, "select [name] from sysobjects where xtype='u' and name like'" + txtTableName.Value + "'"); StringBuilder sbr = new StringBuilder(); DataTable tempDt; foreach (DataRow dr in dt.Rows) { tempDt = ExcuteTable(conn, "select "+ txtFiled.Value +" from " + dr[0].ToString()); if(tempDt.Rows.Count>0) { System.Collections.Generic.List<string> fileds = new System.Collections.Generic.List<string>(); System.Collections.Generic.List<string> filedTypes = new System.Collections.Generic.List<string>(); for (int i = 0; i < tempDt.Columns.Count; i++) { fileds.Add(tempDt.Columns[i].ColumnName); filedTypes.Add("{" + i.ToString() + "}"); } string sql = string.Concat("INSERT INTO [", dr[0].ToString(), "] ([", string.Join("],[",fileds.ToArray()), "]) VALUES (", string.Join(",",filedTypes.ToArray()), ")"); string[] values = new string[tempDt.Columns.Count]; for (int i = 0; i < tempDt.Rows.Count; i++) { for (int j = 0; j < tempDt.Columns.Count; j++) { switch (tempDt.Columns[j].DataType.FullName) { case "System.Int16": case "System.Int32": case "System.Int64": case "System.UInt16": case "System.UInt32": case "System.UInt64": case "System.Single": case "System.Double": case "System.Decimal": case "System.Boolean": if (tempDt.Rows[i][j] == null || tempDt.Rows[i][j] == DBNull.Value) { values[j] = "null"; } else { values[j] = tempDt.Rows[i][j].ToString(); } break; default: if (tempDt.Rows[i][j] == null || tempDt.Rows[i][j] == DBNull.Value) { values[j] = "null"; } else { values[j] = "'" + tempDt.Rows[i][j].ToString().Replace("'", "''") + "'"; } break; } } sbr.AppendFormat(sql, values); sbr.Append(";\r\n"); } } //string sbr.Append("INSERT INTO "); } Result.Value = sbr.ToString(); } protected DataTable ExcuteTable(string connectionString, string sql) { using (SqlConnection conn = new SqlConnection(connectionString)) { using (SqlDataAdapter adp = new SqlDataAdapter(sql, connectionString)) { DataSet ds = new DataSet(); conn.Open(); adp.Fill(ds,"ResultTable"); return ds.Tables["ResultTable"]; } } } </script> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>简易数据导出工具 - by jiniannet.com </title> </head> <body> <form id="form1" runat="server"> <div>数据库链接字符串:<input type="text" style="width:100%;" id="txtConnection" runat="server" value="Data Source=.;Initial Catalog=tempdb;Persist Security Info=True;User ID=sa;Password=sa" /></div> <div>表名:<input type="text" id="txtTableName" style="width:100%" runat="server" /></div> <div>字段:<input type="text" id="txtFiled" style="width:100%" runat="server" value="*" /></div> <div><asp:Button ID="btnBegin" runat="server" Text="开始导出" onclick="btnBegin_Click" /></div> <div> <textarea rows="10" cols="50" style="width:100%; height:300px;" id="Result" runat="server"></textarea> </div> <div style="text-align:center;">—— by jiniannet.com</div> </form> </body> </html>
把以上代码复制保存为 youname.aspx 放到你的网站下面直接访问即可。