简体   繁体   English

如何从C#代码运行sp?

[英]How to run a sp from a C# code?

I am new to ADO.net. 我是ADO.net的新手。 I actually created a sample database and a sample stored procedure. 我实际上创建了一个示例数据库和一个示例存储过程。 I am very new to this concept. 我对这个概念很陌生。 I am not sure of how to make the connection to the database from a C# windows application. 我不确定如何从C#Windows应用程序建立与数据库的连接。 Please guide me with some help or sample to do the same. 请通过一些帮助或样品指导我进行相同的操作。

It sounds like you are looking for a tutorial on ADO.NET. 听起来您正在寻找有关ADO.NET的教程。

Here is one about straight ADO.NET. 这是有关ADO.NET的一种。
Here is another one about LINQ to SQL. 这是有关LINQ to SQL的另一篇文章。

Something like this... (assuming you'll be passing in a Person object) 像这样的东西(假设您将传入一个Person对象)

public int Insert(Person person)
{
SqlConnection conn = new SqlConnection(connStr);
conn.Open();
SqlCommand dCmd = new SqlCommand("InsertData", conn);
dCmd.CommandType = CommandType.StoredProcedure;
try
{
dCmd.Parameters.AddWithValue("@firstName", person.FirstName);
dCmd.Parameters.AddWithValue("@lastName", person.LastName);
dCmd.Parameters.AddWithValue("@age", person.Age);
return dCmd.ExecuteNonQuery();
}
catch
{
throw;
}
finally
{
dCmd.Dispose();
conn.Close();
conn.Dispose();
}
}

This is the usual pattern (it might be a bit different for different databases, Sql Server does not require you to specify the parameters in the command text, but Oracle does, and in Oracle, parameters are prefixed with : not with @) 这是通常的模式(对于不同的数据库,它可能会有所不同,Sql Server不需要您在命令文本中指定参数,但是Oracle则需要,并且在Oracle中,参数以:开头而不是@)

using(var command = yourConnection.CreateCommand())
{
   command.CommandText = "YOUR_SP_CALL(@PAR1, @PAR2)";
   command.CommandType = CommandType.StoredProcedure;
   command.Parameters.Add(new OdbcParameter("@PAR1", "lol"));
   command.Parameters.Add(new OdbcParameter("@PAR2", 1337));
   command.ExecuteNonQuery();
}

Something like this: 像这样:

var connectionString = ConfigurationManager.ConnectionStrings["YourConnectionString"].ConnectionString;

        var conn = new SqlConnection(connectionString);

        var comm = new SqlCommand("YourStoredProc", conn) { CommandType = CommandType.StoredProcedure };

        try
        {
            conn.Open();


            // Create variables to match up with session variables
            var CloseSchoolID = Session["sessCloseSchoolID"];


            //  SqlParameter for each parameter in the stored procedure YourStoredProc
            var prmClosedDate = new SqlParameter("@prmClosedDate", closedDate);
            var prmSchoolID = new SqlParameter("@prmSchoolID", CloseSchoolID);

            // Pass the param values to YourStoredProc
            comm.Parameters.Add(prmClosedDate);
            comm.Parameters.Add(prmSchoolID);

            comm.ExecuteNonQuery();
        }

        catch (SqlException sqlex)
        {

        }

        finally
        {
            conn.Close();
        }

Here is a good starting point http://support.microsoft.com/kb/310130 这是一个很好的起点http://support.microsoft.com/kb/310130

OdbcConnection cn;
OdbcCommand cmd;
OdbcParameter prm;
OdbcDataReader dr;

try {
    //Change the connection string to use your SQL Server.
    cn = new OdbcConnection("Driver={SQL Server};Server=servername;Database=Northwind;Trusted_Connection=Yes");

    //Use ODBC call syntax.
    cmd = new OdbcCommand("{call CustOrderHist (?)}", cn);

    prm = cmd.Parameters.Add("@CustomerID", OdbcType.Char, 5);
    prm.Value = "ALFKI";

    cn.Open();

    dr = cmd.ExecuteReader();

    //List each product.
    while (dr.Read()) 
    Console.WriteLine(dr.GetString(0));

    //Clean up.
    dr.Close();
    cn.Close();
}
catch (OdbcException o) {
    MessageBox.Show(o.Message.ToString());
}

If using SQL Server: 如果使用SQL Server:

SqlConnection connection = new SqlCOnnection("Data Source=yourserver;Initial Catalog=yourdb;user id=youruser;passowrd=yourpassword");
SqlCommand cmd = new SqlCommand("StoredProcName", connection);
cmd.CommandType=StoredProcedureType.Command;
connection.Open();
cmd.ExecuteNonQuery();
connection.Close();

If not then replace Sql with Ole and change the connection string. 如果不是,则用Ole替换Sql并更改连接字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM