zoukankan      html  css  js  c++  java
  • C#连接数据库

    VScode 配置C#开发环境

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    using System.Data;
    using System.Data.SqlClient;
    
    namespace ADODome
    {
        class Program
        {
            static void Main(string[] args)
            {
                         
                string connString_1 = @"server=.SQLEXPRESS;uid=sa;pwd=123456;database=Stu";
                SqlConnection conn = new SqlConnection(connString_1);
                conn.Open();
                if (ConnectionState.Open == conn.State)
                {
                    Console.WriteLine("Connection is Opend!");
                }
                conn.Close();
                if(ConnectionState.Closed == conn.State)
                {
                    Console.WriteLine("Connection is close!");
                }
            }
        }
    }

    封装类

    class SqlHelp
        {
            private static string str = @"server=.SQLEXPRESS;uid=sa;pwd=123456;database=Stu";
    
            public static object GetSingleResult(String sql)
            {
                SqlConnection conn = new SqlConnection(str);
                SqlCommand cmd = new SqlCommand(sql, conn);
                conn.Open();
                object result = cmd.ExecuteScalar();
                conn.Close();
                return result;
            }
    
            public static int Update(String sql)
            {
                SqlConnection conn = new SqlConnection(str);
                SqlCommand cmd = new SqlCommand(sql, conn);
                conn.Open();
                int result = cmd.ExecuteNonQuery();
                conn.Close();
                return result;
            }
    
            public static SqlDataReader GetReader(String sql)
            {
                SqlConnection conn = new SqlConnection(str);
                SqlCommand cmd = new SqlCommand(sql, conn);
                conn.Open();
                //CommandBehavior.CloseConnection
                //不使用conn.close()
                SqlDataReader result = cmd.ExecuteReader(CommandBehavior.CloseConnection);           
                return result;
            }
        }
    调用封装类 

    public int AddStudent( string name) { //"insert into student values('{0}');select @@identity"; string sql = "insert into student values('{0}');select @@identity"; sql = string.Format(sql, name); return SqlHelp.Update(sql); } public void GetStu(int age) { string sql = "select * from student where age="+age; SqlDataReader reader = SqlHelp.GetReader(sql); while (reader.Read()) { Console.WriteLine("age:"+reader["age"] +"; name:"+reader["name"]); } reader.Close(); }
  • 相关阅读:
    OpenCV 学习笔记(1-1)opecv3.41及其扩展库在VS2015下配置
    OpenCV 学习笔记(11)像素级别指针操作
    (19) 树莓派发送微信消息
    mybatis+spring配置
    spring Ioc 实践
    运用BufferedWriter把数据写入文件
    【转】跟我一起学Spring 3(4)–深入理解IoC(控制反转)和DI(依赖注入)
    [转]Spring MVC之@RequestMapping 详解
    python错误处理
    python函数
  • 原文地址:https://www.cnblogs.com/han-guang-xue/p/10522687.html
Copyright © 2011-2022 走看看