1. private void button1_Click(object sender, EventArgs e)
  2. {
  3. try
  4. {
  5.  
  6. //OdbcConnection conn = new OdbcConnection();
  7. //conn.ConnectionString =
  8. // "ODBC;" +
  9. // "Driver={Sybase SQL Anywhere 5.0};" +
  10. // "DefaultDir=c:\myfolder\;" +
  11. // "Dbf=c:\mypath\dbname.db;" +
  12. // "Uid=UserName;" +
  13. // "Pwd=Secret;" +
  14. // "Dsn="""";"; // Must be included!
  15. //conn.Open(); geovindu
  16.  
  17. connectionString = "DSN=geovindu;UID=dba;PWD=sql;"; //Data Source=sademo
  18.  
  19. using (OdbcConnection connection = new OdbcConnection(connectionString))
  20. {
  21. connection.Open();
  22. DataTable dt = new DataTable();// connection.GetSchema();
  23. DataSet ds = new DataSet();
  24. OdbcDataAdapter da = new OdbcDataAdapter("SELECT * FROM employee", connection);
  25. da.Fill(ds);
  26. dt = ds.Tables[0];
  27. // Do work here.
  28. this.dataGridView1.DataSource = dt;
  29. }
  30. }
  31. catch (OdbcException ex)
  32. {
  33. MessageBox.Show(ex.Message.ToString());
  34. }
  35. }

讀取數据如下:  

http://www.dofactory.com/reference/connection-strings

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using Microsoft.Win32;
  9. using System.Data.Odbc;
  10. using System.Data.SqlClient;
  11.  
  12. /*
  13.  
  14. https://msdn.microsoft.com/en-us/library/system.data.odbc.odbcconnection.connectionstring%28v=vs.110%29.aspx
  15. *
  16. * "Driver={SQL Server};Server=(local);Trusted_Connection=Yes;Database=AdventureWorks;"
  17.  
  18. "Driver={Microsoft ODBC for Oracle};Server=ORACLE8i7;Persist Security Info=False;Trusted_Connection=Yes"
  19.  
  20. "Driver={Microsoft Access Driver (*.mdb)};DBQ=c:\bin\Northwind.mdb"
  21.  
  22. "Driver={Microsoft Excel Driver (*.xls)};DBQ=c:\bin\book1.xls"
  23.  
  24. "Driver={Microsoft Text Driver (*.txt; *.csv)};DBQ=c:\bin"
  25.  
  26. "DSN=dsnname"
  27. *
  28. DSN类型有以下三种:
  29.  
  30. 用户DSN:该数据源只能对建立数据源的用户可见.
  31. ODBC用户数据源存贮了如何与指定数据库提供者连接的信息.只对当前用户可见,而且只能用于当前机器上.这里的当前机器是只这个配置只对当前的机器有效,而不是说只能配置本机上的数据库.它可以配置局域网中另一台机器上的数据库.
  32.  
  33. 系统DSN:该数据源对当前机器上所有的用户可见.
  34. ODBC系统数据源存贮了如何指定数据库提供者连接的信息,系统数据对当前机器上的所有用户都是可见的,包括NT服务.也就是说在这里配置的数据源,只要是这台机器的用户都可以访问 .
  35.  
  36. 文件DSN:该数据源对安装了相同驱动的用户可见
  37. 用户DSN只被用户直接使用,它只能用于当前机器中,ASP不能使用它.系统DSN允许所有的用户登陆到特定服务器上去访问数据库,任何具有权限有用户都可以访问系统DSN.在WEB应用程序中访问数据库时,通常都是建立系统DSN. 文件DSN将信息存储在后缀为.dsn的文本文件中,优点是便于移动.
  38.  
  39. 用户DSN只是针对当前用户或者特定用户;系统DSN是底层的,针对全部用户。一般没有特殊情况时,建议使用使用系统DSN,通用性好。
  40. */
  41.  
  42. namespace SQLanyWhereDemo
  43. {
  44.  
  45. /// <summary>
  46. /// 塗聚文 涂聚文
  47. /// </summary>
  48. public partial class Form4 : Form
  49. {
  50.  
  51. RegistryKey regRootKey;
  52. RegistryKey regSubKey;
  53. /// <summary>
  54. ///
  55. /// </summary>
  56. public Form4()
  57. {
  58. InitializeComponent();
  59. }
  60. /// <summary>
  61. ///
  62. /// </summary>
  63. /// <param name="sender"></param>
  64. /// <param name="e"></param>
  65. private void Form4_Load(object sender, EventArgs e)
  66. {
  67. List<string> dsnList = new List<string>();
  68. ///定义Root指向注册表HKEY_LOCAL_MACHINE节点,
  69.  
  70. ///如果是需要获取用户DSN则需要使用 Registry.CurrentUser;
  71. regRootKey = Registry.LocalMachine;
  72.  
  73. ///定义注册表子Path
  74. string strRegPath = @"SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources";
  75. regSubKey = regRootKey.OpenSubKey(strRegPath);
  76. string[] strDSNList = regSubKey.GetValueNames();
  77. foreach (string s in strDSNList)
  78. {
  79. dsnList.Add(s);
  80. }
  81. ///关闭
  82. regSubKey.Close();
  83. regRootKey.Close();
  84.  
  85. this.comboBox1.DataSource = dsnList;
  86. }
  87. /// <summary>
  88. /// is_archive
  89. /// is_archive
  90. /// </summary>
  91. /// <param name="sender"></param>
  92. /// <param name="e"></param>
  93. private void button1_Click(object sender, EventArgs e)
  94. {
  95.  
  96. try
  97. {
  98. string conStr = "DSN=LocalServer";
  99. SqlConnection mCn = new SqlConnection(conStr);
  100. mCn.Open();
  101. }
  102. catch (SqlException ex)
  103. {
  104. MessageBox.Show(ex.Message.ToString());
  105. }
  106.  
  107. }
  108. /// <summary>
  109. ///
  110. /// </summary>
  111. /// <param name="sender"></param>
  112. /// <param name="e"></param>
  113. private void button2_Click(object sender, EventArgs e)
  114. {
  115. try
  116. {
  117.  
  118. string conStr = "DSN=geovindu";//uid=dba;PWD=geovindu" Driver={Sybase SQL Anywhere 5.0};
  119. OdbcConnection conn = new OdbcConnection(conStr);
  120. conn.Open();
  121.  
  122. //OdbcConnection mCn = new OdbcConnection();
  123. //mCn.ConnectionString = "DSN=" + this.comboBox1.Text.Trim();
  124. //mCn.Open();
  125. }
  126. catch (OdbcException ex)
  127. {
  128. MessageBox.Show(ex.Message.ToString());
  129. }
  130. }
  131.  
  132. private const string ODBC_INI_REG_PATH = "SOFTWARE\\ODBC\\ODBC.INI\\";
  133. private const string ODBCINST_INI_REG_PATH = "SOFTWARE\\ODBC\\ODBCINST.INI\\";
  134.  
  135. /// <summary>
  136. /// Creates a new System-DSN entry with the specified values. If the DSN exists, the values are updated.
  137. /// </summary>
  138. /// <param name="dsnName">Name of the DSN for use by client applications</param>
  139. /// <param name="description">Description of the DSN that appears in the ODBC control panel applet</param>
  140. /// <param name="server">Network name or IP address of database server</param>
  141. /// <param name="driverName">Name of the driver to use</param>
  142. /// <param name="trustedConnection">True to use NT authentication, false to require applications to supply username/password in the connection string</param>
  143. /// <param name="database">Name of the datbase to connect to</param>
  144. public static void CreateDSN2(string dsnName, string description, string server, string driverName, bool trustedConnection, string database, string user, string password, string port)
  145. {
  146. // Lookup driver path from driver name
  147. var driverKey = Registry.LocalMachine.CreateSubKey(ODBCINST_INI_REG_PATH + driverName);
  148. if (driverKey == null) throw new Exception(string.Format("ODBC Registry key for driver '{0}' does not exist", driverName));
  149. string driverPath = driverKey.GetValue("Driver").ToString();
  150.  
  151. // Add value to odbc data sources
  152. var datasourcesKey = Registry.LocalMachine.CreateSubKey(ODBC_INI_REG_PATH + "ODBC Data Sources");
  153. if (datasourcesKey == null) throw new Exception("ODBC Registry key for datasources does not exist");
  154. datasourcesKey.SetValue(dsnName, driverName);
  155.  
  156. // Create new key in odbc.ini with dsn name and add values
  157. var dsnKey = Registry.LocalMachine.CreateSubKey(ODBC_INI_REG_PATH + dsnName);
  158. //MessageBox.Show(dsnKey.ToString());
  159. if (dsnKey == null) throw new Exception("ODBC Registry key for DSN was not created");
  160. dsnKey.SetValue("Data Source", dsnName);
  161. dsnKey.SetValue("Database", database);
  162. dsnKey.SetValue("Description", description);
  163. dsnKey.SetValue("Driver", driverPath);
  164. dsnKey.SetValue("Server", server);
  165. dsnKey.SetValue("User name", user);
  166. dsnKey.SetValue("Password", password);
  167. dsnKey.SetValue("Port", port);
  168. dsnKey.SetValue("Trusted_Connection", trustedConnection ? "Yes" : "No");
  169. }
  170. }
  171. }

  ARSoft.Tools.Net2.2.dll  https://github.com/alexreinert/ARSoft.Tools.Net

Csharp: read Sybase SQL anywhere5.5 using c#的更多相关文章

  1. Sybase SQL anywhere5.5

    https://github.com/mono/old-code https://wiki.scn.sap.com/wiki/display/SQLANY/SQL+Anywhere+and+Micro ...

  2. SUP (SAP Mobile SDK 2.2) 连接 Sybase SQL Anywhere sample 数据库

    安装了   SAP Mobile SDK 2.2   后发现,这个版本没有自带Sybase SQL Anywhere  数据库. 解决办法: 1. 免费下载 SQL Anywhere Develope ...

  3. SQL Anywhere5.5: Metadata

    http://dcx.sybase.com/1101/en/dbprogramming_en11/ianywhere-data-sqlanywhere-saconnection-getschem633 ...

  4. csharp:SMO run sql script

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  5. SQL 教程数据库包括:Oracle, Sybase, SQL Server, DB2, Access 等等,您将学到如何使用 SQL 访问和处理数据系统中的数据

    SQL 基础教程 SQL 教程 SQL 简介 SQL 语法 SQL select SQL distinct SQL where SQL AND & OR SQL Order By SQL in ...

  6. sybase sql anywhere 5.0 安装后sybase central中无法打开视图等的解决办法

    无法打开的原因初步分析要用英文版的xp,后来在如下处发现问题,是sql anywhere的版本太旧了, 可能没有使用Unicode编码,设置一下如下选项可以解决问题.

  7. sybase SQL记录

    在一个表中复制一行,主键是MLID ';

  8. [译]流言终结者 —— SQL Server 是Sybase的产品而不是微软的

    http://www.cnblogs.com/xxxtech/archive/2011/12/30/2307859.html by Euan Garden 这些年来我听说过关于这个流言的许多版本,其中 ...

  9. sql: MySQL and Microsoft SQL Server Stored Procedures IN, OUT using csharp code

    MySQL存储过程: #插入一条返回值涂聚文注 DELIMITER $$ DROP PROCEDURE IF EXISTS `geovindu`.`proc_Insert_BookKindOut` $ ...

随机推荐

  1. NET Core微服务之路:自己动手实现Rpc服务框架,基于DotEasy.Rpc服务框架的介绍和集成

    本篇内容属于非实用性(拿来即用)介绍,如对框架设计没兴趣的朋友,请略过. 快一个月没有写博文了,最近忙着两件事;    一:阅读刘墉先生的<说话的魅力>,以一种微妙的,你我大家都会经常遇见 ...

  2. 【C#】 使用Gsof.Native 动态调用 C动态库

    [C#] 使用Gsof.Native 动态调用 C动态库 一.背景 使用C# 开发客户端时候,我们经常会调用一些标准的动态库或是C的类库.虽然C# 提供的PInvoke的方式,但因为使用的场景的多变, ...

  3. 【手记】解决VS发布asp.net项目报错“该项目中不存在目标GatherAllFilesToPublish”及后续问题

    办法在最后. 用VS2017打开一个以前用VS2010写的asp.net项目后,设置好发布选项(发布到文件夹),发布的时候报错如图: 搜索一番,找到的办法是: 在项目文件(xxx.csproj)中,在 ...

  4. InnoDB体系架构(二)内存

    InnoDB体系架构(二)内存 上篇文章 InnoDB体系架构(一)后台线程 介绍了MySQL InnoDB存储引擎后台线程:Master Thread.IO Thread.Purge Thread. ...

  5. 10.TreeSet、比较器

    Comparable和Comparator  Comparable 简介 Comparable 是排序接口.若一个类实现了Comparable接口,就意味着"该类支持排序".  即 ...

  6. 通过用jQuery写一个页面,我学到了什么

    概述 前几天面试,hr发来一个测试文件,让我做做看.我一看,其实就是根据PSD需求写一个页面,再加上一些互动效果即可. 刚好我之前学了切图,jquery等知识还没练手过,于是高兴的答应了. 最后花了3 ...

  7. centos7防火墙管理的变化

    当我们在centos7中输入service iptables status 查看系统的防火墙状态,会出现如下错误: 网上查阅才知道centos7的防火墙管理工具变了,原来的iptables已经不用了, ...

  8. eclipse clean和build作用

    由于eclipse的编译是基于时间戳的判断机制的. 因此当你按build   all的时候有些eclipse认为时间戳没有改变的类不会被编译. 因此你可以先clean一下再编译.这个时候eclipse ...

  9. appium安装完成后运行和执行python脚本的错误合集

    1.第一个错误如下: main.js: error: argument "--app": Expected one argument. null 这个一般是appium服务端安装的 ...

  10. (剑指Offer)面试题45:圆圈中最后剩下的数字

    题目: 0,1,...n-1这n个数字排成一个圆圈,从数字0开始每次从这个圆圈里删除第m个数字,求出这个圆圈里剩下的最后一个数字. 思路: 1.环形链表模拟圆圈 创建一个n个节点的环形链表,然后每次在 ...