【问题标题】:How do I run a stored procedure in WinForms? [closed]如何在 WinForms 中运行存储过程? [关闭]
【发布时间】:2015-07-08 17:26:34
【问题描述】:

我在 C# 中使用 winforms 来启动存储在我的 MS SQL Server 数据库中的过程。我只有一个变量,它是@XmlStr。我有一个包含变量的文本框和一个我想启动该过程的按钮。谁能帮我做到这一点?我整天都在研究这个,到目前为止还没有找到任何对我有用的东西。

【问题讨论】:

标签: c# sql-server winforms stored-procedures


【解决方案1】:
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand("storedProcedureName", conn);
                cmd.CommandType = System.Data.CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@XmlStr", XmlStrVariable);
                cmd.ExecuteNonQuery();
            }

这应该可以帮助您入门。研究 SqlConnection 和 SqlCommand 以获取更多信息。

SqlConnection MSDN

SqlCommand MSDN

【讨论】:

    【解决方案2】:

    希望这会有所帮助:

    string connectionString = "YourConnectionString";
    int parameter = 0;
    using (SqlConnection con = new SqlConnection(connectionString))
    {
        SqlCommand cmd = con.CreateCommand();
        cmd.CommandText = "NameOfYourStoredProcedure";
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("ParameterName", parameter);
    
        try
        {
            con.Open();
            using (SqlDataReader reader = cmd.ExecuteReader())
            {
                while (reader.Read())
                {
                    // Read your reader data
                }
            }
        }
        catch
        {
            throw;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-08
      • 1970-01-01
      • 1970-01-01
      • 2018-09-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多