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();
    }
    }

  • 相关阅读:
    CentOS安装gotop
    oracle 非sys用户创建新用户 授权后 plsql看不到视图
    解决Eclipse+ADT连接夜神模拟器失败问题
    Ext JS v2.3.0 Ext.grid.ColumnModel renderer Record 获取列值
    vi常用快捷键汇总
    外键的变种
    完整性约束
    数据类型2
    数据类型
    表的操作
  • 原文地址:https://www.cnblogs.com/howiehu/p/1857255.html
Copyright © 2011-2022 走看看