C#简单连接sql数据库的方法

作者:吕秀才 时间:2024-01-14 09:01:27 

本文实例讲述了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-WIN2003\\SQLSRV2005;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数据库
0
投稿

猜你喜欢

  • 用Python创建简易网站图文教程

    2023-03-09 20:37:57
  • python爬取免费代理并验证代理是否可用

    2021-12-24 20:02:48
  • ORACLE实例的后台进程

    2009-09-30 10:28:00
  • Python中的异常处理讲解

    2023-01-29 11:22:57
  • 教你用一行Python代码实现GUI图形界面

    2021-10-15 01:38:30
  • 说说CSS的优先权 考虑CSS的继承与层叠

    2008-12-11 13:33:00
  • django3.02模板中的超链接配置实例代码

    2021-07-12 01:02:25
  • JS遮罩层效果 兼容ie firefox jQuery遮罩层

    2024-02-24 23:04:24
  • python 中的命名空间,你真的了解吗?

    2023-12-23 20:19:58
  • ASP调试方法图文教程

    2008-01-26 19:32:00
  • Golang多线程排序实现快速高效地处理大规模数据

    2024-04-27 15:32:29
  • 教你如何在Pycharm中导入requests模块

    2023-05-03 13:10:14
  • vue 自定义指令directives及其常用钩子函数说明

    2024-05-09 10:43:47
  • Javascript: 为<input>设置readOnly属性问题,希望大家以后要小心

    2009-07-23 20:24:00
  • 详情解析TCP与UDP传输协议

    2024-01-02 05:28:57
  • 详解基于Node.js的微信JS-SDK后端接口实现代码

    2024-05-02 17:37:47
  • python3 解决requests出错重试的问题

    2021-02-05 10:26:27
  • 奇怪的PHP引用效率问题分析

    2023-10-29 14:36:04
  • Flask实现swagger在线文档与接口测试流程详解

    2023-04-29 11:34:17
  • 利用webstrom调试Vue.js单页面程序的方法教程

    2023-06-27 01:21:25
  • asp之家 网络编程 m.aspxhome.com