.NET Compact Framework在windows ce 5.0上直接访问桌面版的SQL Server 2005

<!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimSun; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:"Cambria Math"; panose-1:2 4 5 3 5 4 6 3 2 4; mso-font-charset:1; mso-generic-font-family:roman; mso-font-format:other; mso-font-pitch:variable; mso-font-signature:0 0 0 0 0 0;} @font-face {font-family:Calibri; panose-1:2 15 5 2 2 2 4 3 2 4; mso-font-charset:0; mso-generic-font-family:swiss; mso-font-pitch:variable; mso-font-signature:-1610611985 1073750139 0 0 159 0;} @font-face {font-family:新宋体; panose-1:2 1 6 9 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:modern; mso-font-pitch:fixed; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:"/@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:"/@新宋体"; panose-1:2 1 6 9 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:modern; mso-font-pitch:fixed; mso-font-signature:3 135135232 16 0 262145 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-unhide:no; mso-style-qformat:yes; mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; mso-pagination:none; font-size:10.5pt; mso-bidi-font-size:11.0pt; font-family:"Calibri","sans-serif"; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-fareast-font-family:宋体; mso-fareast-theme-font:minor-fareast; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin; mso-bidi-font-family:"Times New Roman"; mso-bidi-theme-font:minor-bidi; mso-font-kerning:1.0pt;} a:link, span.MsoHyperlink {mso-style-priority:99; color:blue; mso-themecolor:hyperlink; text-decoration:underline; text-underline:single;} a:visited, span.MsoHyperlinkFollowed {mso-style-noshow:yes; mso-style-priority:99; color:purple; mso-themecolor:followedhyperlink; text-decoration:underline; text-underline:single;} p.MsoListParagraph, li.MsoListParagraph, div.MsoListParagraph {mso-style-priority:34; mso-style-unhide:no; mso-style-qformat:yes; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; text-indent:21.0pt; mso-char-indent-count:2.0; mso-pagination:none; font-size:10.5pt; mso-bidi-font-size:11.0pt; font-family:"Calibri","sans-serif"; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-fareast-font-family:宋体; mso-fareast-theme-font:minor-fareast; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin; mso-bidi-font-family:"Times New Roman"; mso-bidi-theme-font:minor-bidi; mso-font-kerning:1.0pt;} .MsoChpDefault {mso-style-type:export-only; mso-default-props:yes; mso-bidi-font-family:"Times New Roman"; mso-bidi-theme-font:minor-bidi;} /* Page Definitions */ @page {mso-page-border-surround-header:no; mso-page-border-surround-footer:no;} @page Section1 {size:595.3pt 841.9pt; margin:72.0pt 90.0pt 72.0pt 90.0pt; mso-header-margin:42.55pt; mso-footer-margin:49.6pt; mso-paper-source:0; layout-grid:15.6pt;} div.Section1 {page:Section1;} /* List Definitions */ @list l0 {mso-list-id:2127966426; mso-list-type:hybrid; mso-list-template-ids:-1625365852 888459414 67698713 67698715 67698703 67698713 67698715 67698703 67698713 67698715;} @list l0:level1 {mso-level-text:%1、; mso-level-tab-stop:none; mso-level-number-position:left; margin-left:18.0pt; text-indent:-18.0pt;} ol {margin-bottom:0cm;} ul {margin-bottom:0cm;} -->

.NET Compact Framework windows ce 5.0 上直接访问桌面版的 SQL Server 2005

 

参照 http://blog.csdn.net/upto/archive/2007/09/23/1797126.aspx 的提示和外国朋友的劳动成果。本人英文不好不能翻译只能将自己理解总结如下:

 

环境:

SQL Server 2005 + SQL Server Compcat 3.5 SP1 XP SP3 Virsual Studio 2008

 

1、  配置 SQL Server 2005 远程连接

开始 -> 所有程序 ->Microsoft SQL Server 2005-> 配置工具 ->SQL Server 外围应用配置器 或者

Click On Start | All Programs | Microsoft SQL Server2005 | Configuration Tools | SQL Server Surface Area configuration

设置图片

点击 服务和连接的外围应用配置器 Surface Area Configuration for Services and Connections”

配置图片

按照上面的选项:选择远程连接 -> 选择本地连接和远程连接 -> 选中同时使用 TCP/IP named pipes(B)

点击“应用”或者确定即可。重新启动服务。

 

2、  配置防火墙

开始 -> 控制面板 -> 安全中心 ->Windows 防火墙

 防火墙

点击“添加程序”,选择 C:/Program Files/Microsoft SQL Server/MSSQL.1/MSSQL/Binn/sqlservr.exe 选择确定

 

 

3、  编写程序

 

myConn = new SqlConnection (@"Server=192.168.1.189;Database=C_Gather; User Id=csqlce; Password=CAOchangHONG12345^&*()" );

                myConn.Open();

                SqlDataReader myDataReader;

                SqlCommand myCmd = myConn.CreateCommand();

                myCmd.CommandText = @"SELECT TABLE_SCHEMA, TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE'" ;

                treeView1.BeginUpdate();

                treeView1.Nodes.Clear();

                treeView1.Nodes.Add(new TreeNode ("Tables" ));

                TreeNode childNode = treeView1.Nodes[0];

                int childCount = 0;

                myDataReader = myCmd.ExecuteReader();

                while (myDataReader.Read())

                {

                    string TableName = (myDataReader[0].ToString()) + "." + (myDataReader[1].ToString());

                     childNode.Nodes.Insert(childCount, new TreeNode (TableName));

                    childCount += 1;

                }   

                myDataReader.Close();

                treeView1.Nodes.Add(new TreeNode ("Views" ));

                SqlCommand myCmdView = myConn.CreateCommand();

                myCmdView.CommandText = @"SELECT TABLE_SCHEMA, TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'VIEW'" ;

                TreeNode childNodeView = treeView1.Nodes[1];

                 int childCountView = 0;

                myDataReader = myCmdView.ExecuteReader();

                while (myDataReader.Read())

                {

                    string ViewName = (myDataReader[0].ToString()) + "." + (myDataReader[1].ToString());

                     childNodeView.Nodes.Insert(childCount, new TreeNode (ViewName));

                    childCountView += 1;

                }

                myDataReader.Close();

                treeView1.ExpandAll();

                treeView1.EndUpdate();

                myConn.Close();

            } // try

            catch (SqlException myexception)

            {

                foreach (SqlError err in myexception.Errors)

                {

                    MessageBox .Show(err.Message);

}

}

 

类似上面的代码,你可以到http://www.opennetcf.com/downloads/download.aspx?s=SSMS_Device 下载他的完整代码

 

运行以后在myConn.Open(); 可能出现错误与他dbnetlib.dll 有关的错误。你将 C:/Program Files/Microsoft SQL Server Compact Edition/v3.5/Devices/Client/wce500/x86 文件夹下两个文件

sql.dev.CHS.wce5.x86.CAB sql.wce5.x86.CAB 拷贝到目标设备上安装重新运行即可。

windows ce的开发int startTickCount = Environment.TickCount; // 创建源 SQL Server 数据库连接对象 string srcConnString = "Data Source=(local);Initial Catalog=Northwind;User Id=sa;Password=123;"; SqlConnection srcConnection = new SqlConnection(srcConnString); // 创建目标 SQL Server Compact Edition 数据库连接对象 string destConnString = @"Data Source=Northwind.sdf"; SqlCeConnection destConnection = new SqlCeConnection(destConnString); VerifyDatabaseExists(destConnString); srcConnection.Open(); destConnection.Open(); // 复制数据 CopyTable(srcConnection, destConnection, "SELECT * FROM Products", "Products"); CopyTable(srcConnection, destConnection, "SELECT * FROM Employees", "Employees"); srcConnection.Close(); destConnection.Close(); MessageBox.Show((Environment.TickCount - startTickCount).ToString()); } /// <summary> /// 将源数据库表的数据复制到 SQL Server Compact Edition 数据库的表中。 /// </summary> /// <param name="srcConnection">源数据库连接接对象。</param> /// <param name="destConnection">目标 SQL Server Compact Edition 数据库连接对象。</param> /// <param name="queryString">源数据的查询语句。</param> /// <param name="destTableName">目标数据库表名称。</param> /// <remarks>本方法假设目标 SQL Server Compact Edition 数据库的表已经存在。</remarks> public static void CopyTable( IDbConnection srcConnection, SqlCeConnection destConnection, string queryString, string destTableName) { IDbCommand srcCommand = srcConnection.CreateCommand(); srcCommand.CommandText = queryString; SqlCeCommand destCommand = destConnection.CreateCommand(); destCommand.CommandType = CommandType.TableDirect; //基于表的访问,性能更好 destCommand.CommandText = destTableName; try { IDataReader srcReader = srcCommand.ExecuteReader(); SqlCeResultSet resultSet = destCommand.ExecuteResultSet( ResultSetOptions.Sensitive | //检测对数据源所做的更改 ResultSetOptions.Scrollable | //可以向前或向后滚动 ResultSetOptions.Updatable); //允许更新数据 object[] values; SqlCeUpdatableRecord record; while (srcReader.Read()) { // 从源数据库表读取记录 values = new object[srcReader.FieldCount]; srcReader.GetValues(values); // 把记录写入到目标数据库表 record = resultSet.CreateRecord(); record.SetValues(values); resultSet.Insert(record); } srcReader.Close(); resultSet.Close(); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.ToString()); } } public static void VerifyDatabaseExists(string connectionString) { using (SqlCeConnection connection = new SqlCeConnection(connectionString)) { if (!File.Exists(connection.Database)) { using (SqlCeEngine engine = new SqlCeEngine(connection.ConnectionString)) { engine.CreateDatabase(); string[] commands = Properties.Resources.DbSchema.Split(';'); SqlCeCommand command = new SqlCeCommand(); command.Connection = connection; connection.Open(); string query; for (int i = 0; i < commands.Length; i++) { query = commands[i].Trim(); if (!string.IsNullOrEmpty(query)) { command.CommandText = query; command.ExecuteNonQuery(); } } } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值