zoukankan      html  css  js  c++  java
  • 连接sql server、插入数据、从数据库获取时间(C#)

    using System;
    using System.Data.SqlClient;
    
    namespace Test
    {
        //连接数据库
        public class Connection
        {
            private static string connectionString =
                "Server = 192.168.1.222;" +
                "Database = Test;" +
                "User ID = Test;" +
                "Password = abc123;";
    
            /// <summary>
            /// 连接数据库
            /// </summary>
            /// <returns></returns>
            private SqlConnection ConnectionOpen()
            {
                SqlConnection conn = new SqlConnection(connectionString);
                conn.Open();
                return conn;
            }
    
            /// <summary>
            /// 向表(Table)中插入一条数据
            /// </summary>
            public void Insert(string value1, string value2, string value3, DateTime dateTime)
            {
                SqlConnection conn = ConnectionOpen();
                string sql =
                    "insert into Table(row1, row2, row3, DateTime) values ('" +
                    value1 + "', '" + value2 + "', '" + value3 + "', '" + dateTime + "')";
                SqlCommand comm = new SqlCommand(sql, conn);
                comm.ExecuteReader();
      
    conn.Close(); }
    /// <summary> /// 从数据库中获取当前时间 /// </summary> /// <returns></returns> public DateTime GetDateTimeFromSQL() { SqlConnection conn = ConnectionOpen(); string sql = "select getdate()"; SqlCommand comm = new SqlCommand(sql, conn); SqlDataReader reader = comm.ExecuteReader(); DateTime dt; if (reader.Read()) { dt = (DateTime)reader[0]; conn.Close(); return dt; } conn.Close(); return DateTime.MinValue; } } }
  • 相关阅读:
    Larval API 跨域问题解决
    php常用接口
    phpstudy+nigix+larval伪静态配置
    js、jquery让滚动条到底部
    JS 数组转json和json转数组
    python 基础(十七)内置函数
    python 基础(十六)生成器用法举例
    python 基础(十五)生成器
    python 基础(十四)装饰器
    python基础(十三)模块的定义、导入
  • 原文地址:https://www.cnblogs.com/fengrenyuan/p/3711048.html
Copyright © 2011-2022 走看看