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; } } }
  • 相关阅读:
    UPC-5930 Rest Stops(水题)
    UPC-6199 LCYZ的道路(贪心)
    UPC-6198 JL的智力大冲浪(简单贪心)
    POJ 3279 Filptile dfs
    hrbust 1621 迷宫问题II 广搜
    HDU 1045 dfs + 回溯
    优先队列基本用法
    树。森林。和二叉树之间的转换
    POJ 2689 筛法求素数
    哈理工OJ 1328
  • 原文地址:https://www.cnblogs.com/fengrenyuan/p/3711048.html
Copyright © 2011-2022 走看看