带有存储过程的 C# 中的本地数据库

Local database in C# with stored procedure

我有一个问题,我真的需要你的帮助。请尽快帮助我。这是我的故事。我在 Visual Studio 中为我的项目创建了一个本地数据库。我的意思是我右键单击我的项目并选择添加并将服务基础数据库添加到我的项目中,名称为 OCRDataBase

我的第一个问题是:我在 Visual Studio 的服务器资源管理器菜单中为我的数据库创建了这个存储过程

CREATE PROCEDURE [dbo].[CheckDataBase]
    @Shape int ,
    @p1 int ,
    @p2 int ,
    @p3 int ,
    @p4 int ,
    @output nvarchar(10) out
as
Begin
    select 
        @output = [Character] 
    from 
        dbo.OCRTable 
    where 
        Style = @Shape 
        and Part1 = @p1 and Part2 = @p2 and Part3 = @p3 and Part4 = @p4;

    if(@output is null)
        select @output = '[Unknown]';
End

我有一个 table 有 6 列(id, style, part1, part2, part3, part4,字符)。

这个存储过程应该是 return 个字符,如果没有任何字符匹配存储过程条件,它应该是 return '[Unknown]';

我在 Visual Studio 中编写此函数以获取此存储过程的值 return

static public string Check(int Shape, int p1, int p2, int p3, int p4)
{
            string FunctionOutput = "";

            try
            {
                Connection.Open();
                SqlCommand SqlCommands = new SqlCommand("[dbo].[CheckDataBase]", Connection);
                SqlCommands.CommandType = CommandType.StoredProcedure;
                SqlCommands.Parameters.AddWithValue("@Shape", Shape);
                SqlCommands.Parameters.AddWithValue("@p1", p1);
                SqlCommands.Parameters.AddWithValue("@p2", p2);
                SqlCommands.Parameters.AddWithValue("@p3", p3);
                SqlCommands.Parameters.AddWithValue("@p4", p4);

                SqlParameter OutputParameter = new SqlParameter();
                OutputParameter.ParameterName = "@output";
                OutputParameter.SqlDbType = SqlDbType.NVarChar;
                OutputParameter.Direction = ParameterDirection.Output;
                SqlCommands.Parameters.Add(OutputParameter);

                SqlCommands.ExecuteNonQuery();
                FunctionOutput = OutputParameter.Value.ToString();
                MessageBox.Show(FunctionOutput);
            }
            catch (SqlException exception)
            {
                MessageBox.Show(exception.ToString());
            }
            finally
            {
                Connection.Close();
            }
            return FunctionOutput;
}

但是当 SqlCommand.ExecuteNonQuery(); 执行时,显示此消息:

An unhandled exception of type 'System.InvalidOperationException' occurred in System.Data.dll

Additional information: String[5]: the Size property has an invalid size of 0.

我该怎么办?请帮助我。

我的第二个问题是如何为该数据库提供相对地址,以便我可以从任何文件夹和任何计算机中 运行 我的应用程序。

这是我的数据库连接:

static private SqlConnection Connection = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=D:\Modem Project\PROJECT\OCR\OCR\OCRDataBase.mdf;Integrated Security=True");

我试着写

..\..\OCRDataBase.mdf instead of  D:\Modem Project\PROJECT\OCR\OCR\OCRDataBase.mdf

但这是 Visual Studio 消息:

There is already a database with that name in this folder.

好像Visual Studio想换掉

感谢您的帮助。

根据MSDN,

For output parameters with a variable length type (nvarchar, for example), the size of the parameter defines the size of the buffer holding the output parameter. The output parameter can be truncated to a size specified with Size. For character types, the size specified with Size is in characters.

在您的情况下,您正尝试从存储过程中 return nvarchar,因此请尝试像这样设置大小,

SqlParameter OutputParameter = new SqlParameter();
OutputParameter.ParameterName = "@output";
OutputParameter.SqlDbType = SqlDbType.NVarChar;
OutputParameter.Direction = ParameterDirection.Output;
OutputParameter.Size = 10;
SqlCommands.Parameters.Add(OutputParameter);