zoukankan      html  css  js  c++  java
  • ADO.NET教程(四)

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data;//必须引入
    using System.Data.SqlClient;//必须引入
    
    namespace Command
    {
        class Program
        {
            static void Main(string[] args)
            {
                string connSQL = @"Data Source=.SQLEXPRESS; Initial Catalog=db_MyDemo; Integrated Security=SSPI";//构造连接字符串
                SqlConnectionStringBuilder connStr = new SqlConnectionStringBuilder(connSQL);
    
                using(SqlConnection conn = new SqlConnection(connStr.ConnectionString))
                {
                    //拼接SQL语句
                      StringBuilder strSQL = new StringBuilder();
                    strSQL.Append("insert into tb_SelCustomer ");
                    strSQL.Append("values(");
                    strSQL.Append("'liuhao','0','0','13822223333','liuhaorain@163.com','广东省深圳市宝安区',12.234556,34.222234,'422900','备注信息')");
    
                    Console.WriteLine("Output SQL:
    {0}",strSQL.ToString());
    
                    //创建Command对象
                      SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandType = CommandType.Text;
                    cmd.CommandText = strSQL.ToString();
    
                    try
                    {
                        conn.Open();//一定要注意打开连接
    
                           int rows = cmd.ExecuteNonQuery();//执行命令
                           Console.WriteLine("
    Result: {0}行受影响",rows);
                    }
                    catch(Exception ex)
                    {
                        Console.WriteLine("
    Error:
    {0}", ex.Message);
                    }
                }
    
                Console.Read();
            }
        }
    }
  • 相关阅读:
    C# 操作DataTable
    SQLSERVER 连接常见问题
    python 3 与python 2连接mongoDB的区别
    图片url 设置大小
    Python在VSCode环境抓取TuShare数据存入MongoDB环境搭建
    excel解决日常问题记录
    安装MAT内存分析工具独立版
    类加载机制介绍
    jvm启动语句
    linux监控系统语句
  • 原文地址:https://www.cnblogs.com/aipohoo/p/5428178.html
Copyright © 2011-2022 走看看