从 VisualStudio 生成的数据库内容到程序员生成的内容

发布于 2024-07-13 13:53:32 字数 175 浏览 9 评论 0原文

我希望能够更好地访问数据库,以便我可以执行查询(主要是因为我不理解/不知道它的 API,但我知道 SQL)。 我不想删除 Visual Studio 所做的一切,因为很多内容已经建立在它的基础上,但是我如何才能获得可用于执行 SQL 查询的对象。

这是 Visual Studio 2008、C# 和 MSSQL

I'd like to be able to better access the database so I can execute queries (primarily because I don't understand/know the API for it, but I do know SQL). I don't want to remove everything Visual Studio has done because a lot is already built upon it, but how can I get an object that I can use to execute SQL queries.

This is Visual Studio 2008, C#, and MSSQL

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

你与昨日 2024-07-20 13:53:32

尝试类似这个

using (SqlConnection conn = new SqlConnection("Connection String Goes Here"))
{
    conn.Open();
    using (SqlCommand comm = new SqlCommand("SELECT * FROM TABLE", conn))
    {
        return command.ExecuteScalar() as string;
    }
}

不要忘记添加:

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

Try something like this:

using (SqlConnection conn = new SqlConnection("Connection String Goes Here"))
{
    conn.Open();
    using (SqlCommand comm = new SqlCommand("SELECT * FROM TABLE", conn))
    {
        return command.ExecuteScalar() as string;
    }
}

Don't forget to add:

using System.Data;
using System.Data.SqlClient;
橘味果▽酱 2024-07-20 13:53:32

您还可以查看 数据访问应用程序块 - 只是您在应用程序中安装的 DLL,它允许您执行 SQL 查询,并且更加简单:

DataSet ds = SqlHelper.ExecuteDataset(cs,CommandType.StoredProcedure,"SELECT_DATA"); 

You might also look into the Data Access Application Block - just a DLL you install in your app, which allows you to execute SQL queries and such much more simply:

DataSet ds = SqlHelper.ExecuteDataset(cs,CommandType.StoredProcedure,"SELECT_DATA"); 
辞别 2024-07-20 13:53:32

就我个人而言,我总是会使用免费的 Ideablade DevForce:

http://www.ideablade.com/

并使用根本没有SQL!

Personally, I would always use the free Ideablade DevForce:

http://www.ideablade.com/

And use no SQL at all!

我不在是我 2024-07-20 13:53:32

您是否想知道可以使用哪些 .NET 库对数据库执行 SQL? 如果是这样,请首先查看 SqlConnectionSqlCommand(如果您使用的是 SQL Server,则使用 OleDbConnectionOleDbCommand代码>)。 SqlCommand 有几个 Execute() 方法可以完成您想要的操作。

ADO.NET 是一个相当庞大的野兽,但有大量的快速入门和示例代码。

Are you asking what .NET libraries you can use to execute SQL against a database? If so, start by looking at SqlConnection and SqlCommand (if you're using SQL Server, otherwise use OleDbConnection and OleDbCommand). SqlCommand has several Execute() methods that will do what you're looking for.

ADO.NET is a pretty big beast but there are tons of quickstarts and sample code out there.

请你别敷衍 2024-07-20 13:53:32

不是 100% 确定您真正要求的是什么:-),但假设您有兴趣了解如何以编程方式执行 SQL 查询,您将需要(假设 SQL Server 作为后端):

  • “使用 System.Data ;” 和“使用 System.Data.SqlClient;” using 子句
  • 一个“SqlConnection”对象,用于建立与 SQL Server 的连接(通常与 ConnectionString 组合,存储在某种形式的配置文件中)
  • 一个“SqlCommand”对象,用于制定 SQL 查询并
  • 以某种处理可能结果的方式 执行它从该查询中

你会得到类似的结果:

SqlConnection connection = new SqlConnection(-connection string-);

SqlCommand command = new SqlCommand("...your sql query here...", connection);

connection.Open();

command.ExecuteScalar / command.ExecuteReader / command.ExecuteNonQuery
(depending on your needs)

connection.Close();

当然还有一些错误处理......:-)

这有帮助吗?

Not 100% sure what you're really asking for :-), but assuming you're interested in knowing how to programatically execute a SQL query, you'll need (assuming SQL Server as the backend):

  • a "using System.Data;" and "using System.Data.SqlClient;" using clause
  • a "SqlConnection" object to establish your connection to SQL Server (usually combined with a ConnectionString, stored in some form of a config file)
  • a "SqlCommand" object to formulate your SQL query and execute it
  • some way of dealing with possible results from that query

You'll have something like:

SqlConnection connection = new SqlConnection(-connection string-);

SqlCommand command = new SqlCommand("...your sql query here...", connection);

connection.Open();

command.ExecuteScalar / command.ExecuteReader / command.ExecuteNonQuery
(depending on your needs)

connection.Close();

plus of course, some error handling.... :-)

Does that help that at all??

淡莣 2024-07-20 13:53:32

从你的问题中我不确定你在做什么,但如果你只是想学习如何使用 SqlConnection、SqlCommand 和 DataReader 对象从数据库中检索项目,请查看:

http://msdn.microsoft.com/en-us/library/haa3afyz (VS.71).aspx

I'm not sure from your question what you've got going on, but if you just want to learn how to use a SqlConnection, SqlCommand, and a DataReader object to retrieve items from a database, check this out:

http://msdn.microsoft.com/en-us/library/haa3afyz(VS.71).aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文