c# - VS2015 C# 中的 SqlConnection

标签 c# sql visual-studio-2015 database-connection

我对 C# web 开发(或任何与此相关的开发)非常陌生,但我正在尝试弄清楚如何将 SQL 查询的结果保存到变量中。我想我理解这个过程,但是我在 Web 上找到的许多示例都使用了 SqlConnection 语句。我的 Visual Studio 副本似乎没有该命令(很确定我在这里使用了错误的词)。我在软件方面或知识方面缺少什么来完成我的任务?

预先感谢您的帮助。 部门

最佳答案

这取决于你想做什么:插入、更新、获取数据。它还取决于您是否要使用 ORM 库。我都依赖。我在下面复制的代码是如何使用 Ado.Net 检索 DataTable 的示例(正如您提到的 SqlConnection):

你必须使用:

using System.Data;
using System.Data.SqlClient;

这是检索数据表的代码

    private DataSet ExecuteDataset(string query)
    {
        var conn = new SqlConnection("Data Source=" + Server + ";Initial Catalog=" + Database + ";User Id=" + Username + ";Password=" + Password + ";");
        DataSet ds;
        try
        {
            conn.Open();
            ds = new DataSet();
            var da = new SqlDataAdapter(query, conn);
            da.Fill(ds);
        }
        catch (Exception)
        {
            throw;
        }
        finally
        {
            conn.Dispose();
            conn.Close();
        }
        return ds;
    }

       private DataSet ExecuteDataset(string query, SqlParameter[] parametros)
        {
            var conn = new SqlConnection("Data Source=" + Server + ";Initial Catalog=" + Database + ";User Id=" + Username + ";Password=" + Password + ";");
            DataSet ds;
            try
            {
                conn.Open();

                SqlCommand command = conn.CreateCommand();
                command.CommandText = query;

                foreach (SqlParameter p in parametros)
                {
                    command.Parameters.Add(p);
                }

                ds = new DataSet();
                var da = new SqlDataAdapter(command);
                da.Fill(ds);
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                conn.Dispose();
                conn.Close();
            }
            return ds;
        }

这是运行不期望有参数和无参数结果的查询的代码:

    private void ExecuteNonQuery(string query)
    {
        var conn = new SqlConnection("Data Source=" + Server + ";Initial Catalog=" + Database + ";User Id=" + Username + ";Password=" + Password + ";");
        try
        {
            conn.Open();
            SqlCommand command = conn.CreateCommand();
            command.CommandText = query;
            command.ExecuteNonQuery();
        }
        catch (Exception)
        {
            throw;
        }
        finally
        {
            conn.Dispose();
            conn.Close();
        }
    }

    private void ExecuteNonQuery(string query, SqlParameter[] parametros)
    {
        var conn = new SqlConnection("Data Source=" + Server + ";Initial Catalog=" + Database + ";User Id=" + Username + ";Password=" + Password + ";");
        try
        {
            conn.Open();
            SqlCommand command = conn.CreateCommand();
            command.CommandText = query;

            foreach (SqlParameter p in parametros)
            {
                command.Parameters.Add(p);
            }

            command.ExecuteNonQuery();
        }
        catch (Exception)
        {
            throw;
        }
        finally
        {
            conn.Dispose();
            conn.Close();
        }
    }

关于c# - VS2015 C# 中的 SqlConnection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41493175/

相关文章:

sql - PostgreSQL 哈希索引

sql - 在sql中的组表达式中使用 "where"

mysql - 带参数的MySQL函数出错

c# - Visual Studio 项目备份

c# - 更改 PropertyGrid 左侧集合编辑器/ View 的宽度

c# - 关于 C# 中的 DllImport

c++ - Visual Studio : project is not up to date "because "AlwaysCreate"was specified"?

visual-studio-2015 - 使用 VS2015 工具(预览版)安装 .NET Core 1.0 后,我无法创建 .NET Core 类库项目

c# - .NET 6 Parallel.ForEachAsync 中需要两个 token 吗?

c# - 何时使用属性以及何时使用子属性?