SQL 服务器在内部并使用存储过程

SQL Server within & using stored procedure

尝试创建一个 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;
    }
}

第一名 我认为你连接到错误的数据库,可能你在 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;

编辑 1: 这是将数据库名称放入连接字符串的方式:

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