手机
当前位置:查字典教程网 >编程开发 >C#教程 >C#简单连接sql数据库的方法
C#简单连接sql数据库的方法
摘要:本文实例讲述了C#简单连接sql数据库的方法。分享给大家供大家参考,具体如下:usingSystem;usingSystem.Collect...

本文实例讲述了C#简单连接sql数据库的方法。分享给大家供大家参考,具体如下:

using System; using System.Collections.Generic; using System.Text; //数据库操作对象库 using System.Data; using System.Data.SqlClient; using worddic; namespace testDB { class Program { static void Main(string[] args) { //char[] array = new char[2] ; string [] a = new string[2]; FileOp words = new FileOp(); words.openfile("worddic.txt"); words.getwords(ref a); /* Console.WriteLine("请输入Data Source:"); string data_source = Console.ReadLine(); Console.WriteLine("请输入Initial Catalog:"); string initial_catalog = Console.ReadLine(); Console.WriteLine("请输入user id:"); string user_id = Console.ReadLine(); Console.WriteLine("请输入pass word:"); string pword = Console.ReadLine(); //连接字符串 string strConn ="Data Source="+data_source+";Initial Catalog="+initial_catalog+";User ID="+user_id+";Password="+pword+"";//YourPwd替换为你设置的sa账户密码 */ string strConn = "Data Source=HYPER-V-WIN2003SQLSRV2005;Initial Catalog=Mytest;User ID=sa;Password=sa"; SqlConnection conn = null; SqlCommand sqlCmd = null; try { //创建connection对象 conn = new SqlConnection(strConn); //打开数据库连接 conn.Open(); //创建Transac Sql命令对象 sqlCmd = conn.CreateCommand(); //创建建表语句 //sqlCmd.CommandText = "create table wordlist(wrongwords varchar(30),rightwords varchar(30),sign char(20),)"; //sqlCmd.ExecuteScalar(); while (a[0] != null) { sqlCmd.CommandText ="insert into wordlist(wrongwords,rightwords,sign) values('" + a[0] + "','" + a[1] + "',1 )"; sqlCmd.ExecuteScalar(); a[0] = null; a[1] = null; words.getwords(ref a); } words.fileclose(); Console.WriteLine(); //打印所有记录 } catch (SqlException e) { Console.WriteLine(e.Message); } finally { conn.Close(); } Console.WriteLine("程序结束,按任意键退出"); Console.ReadKey(); } } }

希望本文所述对大家C#程序设计有所帮助。

【C#简单连接sql数据库的方法】相关文章:

C#读写文件的方法汇总

浅析C# web访问mysql数据库-整理归纳总结

C# 中将数值型数据转换为字节数组的方法

C# dynamic关键字的使用方法

C# IP地址与整数之间转换的具体方法

深入理解C#实现快捷键(系统热键)响应的方法

C# 大数据导出word的假死报错的处理方法

C#中判断本地系统的网络连接状态的方法

C# byte数组与Image相互转换的方法

C#生成sitemap站点地图的方法

精品推荐
分类导航