Csharp: read Sybase SQL anywhere5.5 using c#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | private void button1_Click( object sender, EventArgs e) { try { //OdbcConnection conn = new OdbcConnection(); //conn.ConnectionString = // "ODBC;" + // "Driver={Sybase SQL Anywhere 5.0};" + // "DefaultDir=c:\myfolder\;" + // "Dbf=c:\mypath\dbname.db;" + // "Uid=UserName;" + // "Pwd=Secret;" + // "Dsn="""";"; // Must be included! //conn.Open(); geovindu connectionString = "DSN=geovindu;UID=dba;PWD=sql;" ; //Data Source=sademo using (OdbcConnection connection = new OdbcConnection(connectionString)) { connection.Open(); DataTable dt = new DataTable(); // connection.GetSchema(); DataSet ds = new DataSet(); OdbcDataAdapter da = new OdbcDataAdapter( "SELECT * FROM employee" , connection); da.Fill(ds); dt = ds.Tables[0]; // Do work here. this .dataGridView1.DataSource = dt; } } catch (OdbcException ex) { MessageBox.Show(ex.Message.ToString()); } } |
讀取數据如下:
http://www.dofactory.com/reference/connection-strings
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 | using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using Microsoft.Win32; using System.Data.Odbc; using System.Data.SqlClient; /* https://msdn.microsoft.com/en-us/library/system.data.odbc.odbcconnection.connectionstring%28v=vs.110%29.aspx * * "Driver={SQL Server};Server=(local);Trusted_Connection=Yes;Database=AdventureWorks;" "Driver={Microsoft ODBC for Oracle};Server=ORACLE8i7;Persist Security Info=False;Trusted_Connection=Yes" "Driver={Microsoft Access Driver (*.mdb)};DBQ=c:\bin\Northwind.mdb" "Driver={Microsoft Excel Driver (*.xls)};DBQ=c:\bin\book1.xls" "Driver={Microsoft Text Driver (*.txt; *.csv)};DBQ=c:\bin" "DSN=dsnname" * DSN类型有以下三种: 用户DSN:该数据源只能对建立数据源的用户可见. ODBC用户数据源存贮了如何与指定数据库提供者连接的信息.只对当前用户可见,而且只能用于当前机器上.这里的当前机器是只这个配置只对当前的机器有效,而不是说只能配置本机上的数据库.它可以配置局域网中另一台机器上的数据库. 系统DSN:该数据源对当前机器上所有的用户可见. ODBC系统数据源存贮了如何指定数据库提供者连接的信息,系统数据对当前机器上的所有用户都是可见的,包括NT服务.也就是说在这里配置的数据源,只要是这台机器的用户都可以访问 . 文件DSN:该数据源对安装了相同驱动的用户可见 用户DSN只被用户直接使用,它只能用于当前机器中,ASP不能使用它.系统DSN允许所有的用户登陆到特定服务器上去访问数据库,任何具有权限有用户都可以访问系统DSN.在WEB应用程序中访问数据库时,通常都是建立系统DSN. 文件DSN将信息存储在后缀为.dsn的文本文件中,优点是便于移动. 用户DSN只是针对当前用户或者特定用户;系统DSN是底层的,针对全部用户。一般没有特殊情况时,建议使用使用系统DSN,通用性好。 */ namespace SQLanyWhereDemo { /// <summary> /// 塗聚文 涂聚文 /// </summary> public partial class Form4 : Form { RegistryKey regRootKey; RegistryKey regSubKey; /// <summary> /// /// </summary> public Form4() { InitializeComponent(); } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Form4_Load( object sender, EventArgs e) { List< string > dsnList = new List< string >(); ///定义Root指向注册表HKEY_LOCAL_MACHINE节点, ///如果是需要获取用户DSN则需要使用 Registry.CurrentUser; regRootKey = Registry.LocalMachine; ///定义注册表子Path string strRegPath = @"SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources" ; regSubKey = regRootKey.OpenSubKey(strRegPath); string [] strDSNList = regSubKey.GetValueNames(); foreach ( string s in strDSNList) { dsnList.Add(s); } ///关闭 regSubKey.Close(); regRootKey.Close(); this .comboBox1.DataSource = dsnList; } /// <summary> /// is_archive /// is_archive /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button1_Click( object sender, EventArgs e) { try { string conStr = "DSN=LocalServer" ; SqlConnection mCn = new SqlConnection(conStr); mCn.Open(); } catch (SqlException ex) { MessageBox.Show(ex.Message.ToString()); } } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button2_Click( object sender, EventArgs e) { try { string conStr = "DSN=geovindu" ; //uid=dba;PWD=geovindu" Driver={Sybase SQL Anywhere 5.0}; OdbcConnection conn = new OdbcConnection(conStr); conn.Open(); //OdbcConnection mCn = new OdbcConnection(); //mCn.ConnectionString = "DSN=" + this.comboBox1.Text.Trim(); //mCn.Open(); } catch (OdbcException ex) { MessageBox.Show(ex.Message.ToString()); } } private const string ODBC_INI_REG_PATH = "SOFTWARE\\ODBC\\ODBC.INI\\" ; private const string ODBCINST_INI_REG_PATH = "SOFTWARE\\ODBC\\ODBCINST.INI\\" ; /// <summary> /// Creates a new System-DSN entry with the specified values. If the DSN exists, the values are updated. /// </summary> /// <param name="dsnName">Name of the DSN for use by client applications</param> /// <param name="description">Description of the DSN that appears in the ODBC control panel applet</param> /// <param name="server">Network name or IP address of database server</param> /// <param name="driverName">Name of the driver to use</param> /// <param name="trustedConnection">True to use NT authentication, false to require applications to supply username/password in the connection string</param> /// <param name="database">Name of the datbase to connect to</param> public static void CreateDSN2( string dsnName, string description, string server, string driverName, bool trustedConnection, string database, string user, string password, string port) { // Lookup driver path from driver name var driverKey = Registry.LocalMachine.CreateSubKey(ODBCINST_INI_REG_PATH + driverName); if (driverKey == null ) throw new Exception( string .Format( "ODBC Registry key for driver '{0}' does not exist" , driverName)); string driverPath = driverKey.GetValue( "Driver" ).ToString(); // Add value to odbc data sources var datasourcesKey = Registry.LocalMachine.CreateSubKey(ODBC_INI_REG_PATH + "ODBC Data Sources" ); if (datasourcesKey == null ) throw new Exception( "ODBC Registry key for datasources does not exist" ); datasourcesKey.SetValue(dsnName, driverName); // Create new key in odbc.ini with dsn name and add values var dsnKey = Registry.LocalMachine.CreateSubKey(ODBC_INI_REG_PATH + dsnName); //MessageBox.Show(dsnKey.ToString()); if (dsnKey == null ) throw new Exception( "ODBC Registry key for DSN was not created" ); dsnKey.SetValue( "Data Source" , dsnName); dsnKey.SetValue( "Database" , database); dsnKey.SetValue( "Description" , description); dsnKey.SetValue( "Driver" , driverPath); dsnKey.SetValue( "Server" , server); dsnKey.SetValue( "User name" , user); dsnKey.SetValue( "Password" , password); dsnKey.SetValue( "Port" , port); dsnKey.SetValue( "Trusted_Connection" , trustedConnection ? "Yes" : "No" ); } } } |
ARSoft.Tools.Net2.2.dll https://github.com/alexreinert/ARSoft.Tools.Net
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)
分类:
数据库编程
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
2017-02-08 Python:dictionary