• 怎样引用类中非静态的方法


     1 using System;
     2 using System.Collections.Generic;
     3 using System.Data;
     4 using System.Data.SqlClient;
     5 using System.Linq;
     6 using System.Text;
     7 using System.Threading.Tasks;
     8 
     9 namespace ConsoleApplication3
    10 {
    11     class Program
    12     {
    13         //非静态方法
    14         public SqlConnection createSqlConnection()
    15         {
    16             //数据库连接字符串
    17             string sql = "Data Source = localhost;DataBase=db_MyDemo;User Id=sa;Password=123456";
    18             SqlConnection con = new SqlConnection(sql);
    19             con.Open();
    20             return con;
    21         }
    22 
    23         //静态方法
    24         public static int InsertAll()
    25         {
    26             //拼接sql语句
    27             StringBuilder strSQL = new StringBuilder();
    28             strSQL.Append("insert into tb_SelCustomer ");
    29             strSQL.Append("values(");
    30             strSQL.Append("'liuhao','0','0','13822223333','liuhaorain@163.com','广东省深圳市宝安区',12.234556,34.222234,'422900','备注信息')");
    31             Console.WriteLine("Output SQL:
    {0}", strSQL.ToString());
    32             //创建Command对象
    33             SqlCommand cmd = new SqlCommand();
    34 
    35             //静态方法调用非静态方法
    36             cmd.Connection =new Program().createSqlConnection();
    37             cmd.CommandType = CommandType.Text;
    38             cmd.CommandText = strSQL.ToString();
    39             int rows = cmd.ExecuteNonQuery();
    40             return rows;
    41         }
    42 
    43         static void Main(string[] args)
    44         {
    45             try
    46             {
    47                 Console.WriteLine("受影响的行数为: {0}", InsertAll());
    48             }
    49             catch (Exception ex)
    50             {
    51                 Console.WriteLine("
    Error:
    {0}", ex.Message);
    52             }
    53             Console.Read();
    54         }
    55 
    56     }
    57 }
  • 相关阅读:
    求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。
    getchar函数
    计算机网络04-ip与子网划分
    计算机网络03-传输层、可靠数据传输、UDP与TCP
    计算机网络02-应用层(http、email、dns)
    游戏-图形学学习路线
    markDown 入门
    webpack 入门级 傻瓜式教学
    npm 切换 cnpm 切换淘宝镜像源
    vue 父组件在接收子组件的同时传递一个当前的数据
  • 原文地址:https://www.cnblogs.com/shouyeren/p/6054871.html
Copyright © 2020-2023  润新知