【问题标题】:SQL Server within & using stored procedureSQL Server 内和使用存储过程
【发布时间】:2015-01-07 21:00:33
【问题描述】:

尝试创建一个用于在家学习目的的 asp.net c# 表单,但我绝对难以连接到我的存储过程。

我肯定在正确的数据库中,因为当我通过 cmdprompt 启动数据库并通过可视化设计中的数据源连接到它时,它会连接并找到存储过程。所以一定有什么我做错了吗?我已经在 Google 上搜索了大约 30-40 分钟,但我尝试过的一切都没有解决我的问题。请问有什么建议吗?

const string constring = @"Data Source=(localdb)\ProjectsV12;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False";

    public static int InsertEnquiry(string Name, string Subject, string Email, string Message, string Phone) {

        //default ticketid of 0 which will be changed to -1 if an error or greater than 0 if successful
        int TicketID = 0;

        if (String.IsNullOrEmpty(Phone)) Phone = "0";

        using (var conn = new SqlConnection(constring)) {
            //create command
            SqlCommand command = new SqlCommand();

            //tell the command which connection its using
            command.Connection = conn;

            //inform command that it is a stored procedure and the name of stored procedure
            command.CommandText = "InsertEnquiry";
            command.CommandType = CommandType.StoredProcedure;

            //add the parameters to the sqlcommand
            command.Parameters.Add(new SqlParameter("@Name", SqlDbType.NVarChar)).Value = Name;
            command.Parameters.Add(new SqlParameter("@Subject", SqlDbType.NVarChar)).Value = Subject;
            command.Parameters.Add(new SqlParameter("@Phone", SqlDbType.NVarChar)).Value = Phone;
            command.Parameters.Add(new SqlParameter("@Email", SqlDbType.NVarChar)).Value = Email;
            command.Parameters.Add(new SqlParameter("@Message", SqlDbType.NVarChar)).Value = Message;

            // try run command and set TicketID to the row inserted into the table.
            try {
                conn.Open();
                //run command
                command.ExecuteNonQuery();

                //return scope identity of row.
                TicketID = (int)command.Parameters["@TicketID"].Value;
            }
            catch (Exception e) {
                //show -1 to state there is an error
                TicketID = -1;
            }
        }

        return TicketID;
    }
}

【问题讨论】:

  • 这是一篇 MSDN 文章。您可能需要向下滚动。 Using Parameters With a SqlCommand and a Stored Procedure
  • 没有 SQL Server 2013 版本.... 我们有 SQL Server 2012 和 2014 .... 你用的是哪一个?
  • 对不起 - 它的视觉表达 2013 在里面使用 sql server - 如果可能的话我会修改标题。
  • 你遇到什么样的错误?
  • 找不到存储过程“InsertEnquiry”。

标签: c# asp.net sql-server stored-procedures ado.net


【解决方案1】:

第一 我认为您连接到错误的数据库,可能您在 master 中。 运行这段代码并检查数据库的名称,看看它是否是您想要的。

public static string checkDB()
{
    string dbName = "";
    using (var conn = new SqlConnection(constring))
    {
        //create command
        SqlCommand command = new SqlCommand();

        //tell the command which connection its using
        command.Connection = conn;

        //inform command that it is a stored procedure and the name of stored procedure
        command.CommandText = "select DB_NAME()";
        command.CommandType = CommandType.Text;

        // try run command and set TicketID to the row inserted into the table.
        try
        {
            conn.Open();
            //run command
            SqlDataReader reader = command.ExecuteReader();
            reader.Read();
            dbName = reader[0].ToString();
        }
        catch (Exception e)
        {
        }
    }

    return dbName;
}

第二 您正在尝试从参数 @TicketID 获取值,但您没有将此参数指定为输出参数。

command.Parameters.Add("@TicketID", SqlDbType.Int).Direction = ParameterDirection.Output;

EDIT1: 这是如何将数据库名称放入连接字符串中的:

const string constring = @"Data Source=(localdb)\ProjectsV12;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;Initial Catalog=MY_DB_NAME";

【讨论】:

  • 非常感谢!我只知道我错过了一些东西!你是对的 - 它没有连接到正确的目录。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-08
相关资源
最近更新 更多