zoukankan      html  css  js  c++  java
  • ADO.NET 学习笔记

    1、连接到 SQL Server:

    // Assumes connectionString is a valid connection string.
    using (SqlConnection connection = new SqlConnection(connectionString))
    {
    connection.Open();
    // Do work here.
    }

    2、执行命令 (ADO.NET)

    下面的代码示例演示如何创建 SqlCommand 对象以通过设置其属性执行存储过程。 SqlParameter 对象用于指定存储过程的输入参数。 使用 ExecuteReader 方法执行此命令,并在控制台窗口中显示 SqlDataReader 的输出。

    代码
    static void GetSalesByCategory(string connectionString, string categoryName)
    {
    using (SqlConnection connection = new SqlConnection(connectionString))
    {
    // Create the command and set its properties.
    SqlCommand command = new SqlCommand();
    command.Connection
    = connection;
    command.CommandText
    = "SalesByCategory";
    command.CommandType
    = CommandType.StoredProcedure;

    // Add the input parameter and set its properties.
    SqlParameter parameter = new SqlParameter();
    parameter.ParameterName
    = "@CategoryName";
    parameter.SqlDbType
    = SqlDbType.NVarChar;
    parameter.Direction
    = ParameterDirection.Input;
    parameter.Value
    = categoryName;

    // Add the parameter to the Parameters collection.
    command.Parameters.Add(parameter);

    // Open the connection and execute the reader.
    connection.Open();
    SqlDataReader reader
    = command.ExecuteReader();

    if (reader.HasRows)
    {
    while (reader.Read())
    {
    Console.WriteLine(
    "{0}: {1:C}", reader[0], reader[1]);
    }
    }
    else
    {
    Console.WriteLine(
    "No rows found.");
    }
    reader.Close();
    }
    }

  • 相关阅读:
    Mysql 视图笔记2
    mysql 触发器
    Mysql 视图笔记
    最详细的Log4j使用教程
    java制作简单的坦克大战
    Java数据库连接池的几种配置方法(以MySQL数据库为例)
    几种破解MySQL root密码的几种方法:
    Django URL的命令空间
    python调用jar包类
    (http://fonts.googleapis.com/css?)打开很慢解决方案
  • 原文地址:https://www.cnblogs.com/howiehu/p/1857255.html
Copyright © 2011-2022 走看看