C#Application / SQL Server 2008连接 [英] C# Application / SQL Server 2008 connection

查看:77
本文介绍了C#Application / SQL Server 2008连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



想要创建一个带有SQL Server 2008连接的示例C#应用程序。



我在SQL 2008中创建了一个DatabaseTable - 使用C#Code创建并打开了一个连接。



我的任务是创建一个新记录并将其保存到SQL 2008(我的数据库)。



我想知道的是:



(i)我如何只使用存储过程执行此操作



(ii)如何调用存储过程从我的代码。



任何关于此的帮助将非常感激!!在此先感谢:)

Hello,

Would like to create a sample C# application with SQL Server 2008 connection.

I have a created a DatabaseTable in SQL 2008 - created and opened a connection using C# Code.

My task is to create a New Record and save it to the SQL 2008 (my DB).

What i would like to know is:

(i) How can i perform this only by using a Stored Procedure

(ii) How to call the Stored Procedure from my code.

Any help regarding this will be appreciated a lot!! Thanks in advance :)

推荐答案

在您的代码中,如果您输入的连接字符串正确,则以下代码应该这样做;

In your code if you typed your connection string correct the following code should do it;
using (var conn = new SqlConnection(connectionString))
using (var command = new SqlCommand("ProcedureName", conn) 
{
    CommandType = CommandType.StoredProcedure }) {
    conn.Open();
    command.ExecuteNonQuery();
    conn.Close();
}





如果您需要在 StoredProcedure中使用参数只需将参数添加到SqlCommand对象的Parameters集合中。

类似:



If you need to use parameters in your StoredProcedure just add the parameters to the Parameters collection of the SqlCommand object.
Like:

command.Parameters.AddWithValue("paramName", "paramValue");



祝你好运,

OI


Good luck,
OI


Hello Freind ,



请查看此链接以了解商店程序及其语法



Sql Server - 如何在Sql server中编写存储过程 [ ^ ]



http://blog.sqlauthority.com/2010/06/02/sql-server-stored-procedure-and-transactions/ [< a href =http://blog.sqlauthority.com/2010/06/02/sql-server-stored-procedure-and-transactions/target =_ blanktitle =New Window> ^ ]



http://www.mssqltips.com/sqlservertutorial/162/how-to-create-a-sql-server-stored-procedure-with-parameters/ [ ^ ]





并将其与您的代码一起使用请查看此链接....

如何调用存储过程asp.net c#代码 [ ^ ]



在ADO.NET中调用存储过程 [ ^ ]
Hello Freind,

please review this link to know about store procedure and its syntax

Sql Server - How to write a Stored procedure in Sql server[^]

http://blog.sqlauthority.com/2010/06/02/sql-server-stored-procedure-and-transactions/[^]

http://www.mssqltips.com/sqlservertutorial/162/how-to-create-a-sql-server-stored-procedure-with-parameters/[^]


and for using it with your code please review this links....
how to call stored procedure in asp.net c# code[^]

Calling Stored procedures in ADO.NET[^]




查看此链接以创建执行crud操作的过程。

1-) http://www.novicksoftware.com/Articles/crud- operations-using-sql-server-stored-procedures-part-2.htm [ ^ ]



2) http://www.pawlowski.cz/2011/01/automating-crud-procedures-generation-t-sql/ [ ^ ]



您可以像这样打电话给您的程序;



Hi,
Look at this links for creating procedures that are perform crud operations.
1-) http://www.novicksoftware.com/Articles/crud-operations-using-sql-server-stored-procedures-part-2.htm[^]

2-) http://www.pawlowski.cz/2011/01/automating-crud-procedures-generation-t-sql/[^]

And you may call your procedure like this;

using (SqlConnection conn = new SqlConnection(getConnectionString()))
using (SqlCommand cmd = conn.CreateCommand())
{
    cmd.CommandText = parameterStatement.getQuery();
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue("SeqName", "SeqNameValue");

    var returnParameter = cmd.Parameters.Add("@ReturnVal", SqlDbType.Int);
    returnParameter.Direction = ParameterDirection.ReturnValue;

    conn.Open();
    cmd.ExecuteNonQuery();
    var result = returnParameter.Value;
}







此代码是调用程序的示例。你可以为你的程序改变参数。



希望这会对你有所帮助,

Ibrahim Uylas




This code is example for calling procedure. You may change paramters for your procedure.

Hope this will help you,
Ibrahim Uylas


这篇关于C#Application / SQL Server 2008连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆