将变量传递给存储过程c#。

时间:2022-01-27 01:11:52

I have a variable "keypress" that gets set when a phone call is made into our system. The way it is written to our text file is as follows:

我有一个变量“keypress”,当一个电话被打进我们的系统时,它会被设置。它被写入我们的文本文件的方式如下:

w.Write(attrColl["keypress"].Value);

What I need to do is pass these keypresses as they come in, to a sproc.

我需要做的是把这些按键输入到sproc。

So, my first question is how do I pass it to the sproc, the code I have looks like this:

我的第一个问题是如何将它传递给sproc,我的代码是这样的:

mySqlCommand.Parameters.Add("@Q1", SqlDbType.Int).Value = keypress;
mySqlCommand.Parameters.Add("@Q2", SqlDbType.Int).Value = keypress;

for example: if the first keypress is 500#, and the second keypress is 300#. it should look like @Q1 = 500#, and @Q2 = 300#

例如:如果第一个按键是500#,第二个按键是300#。它应该看起来像@Q1 = 500#, @Q2 = 300#

Do I need to convert the keypress to a string first? If so, what is the best way of doing that, as I have about 10 questions that are given so I have 10 unique keypresses that come back.

我需要先将按键转换为字符串吗?如果有,最好的方法是什么,因为我有10个问题,所以我有10个唯一的按键。

And second, because there is a # sign at the end of every keypress, will the sproc input be a nvarchar as opposed to a int? Or what is the best way to handle a #.

其次,因为每个按键末尾都有一个#符号,sproc输入将是一个nvarchar而不是int?或者处理#的最好方法是什么。

Any thoughts, suggestion, or documentation is appreciated.

感谢您的任何想法、建议或文件。

1 个解决方案

#1


3  

I've assumed that your text file looks a little something like this, all on one line.

我假设你的文本文件看起来像这样,都在一行上。

100#400#900#

100 # 400 # 900 #

I also assume that you've read this line back into a variable of somename.

我还假设你已经把这一行读回到somename的变量中。

so

所以

        var keypressline = "100#400#900#";
        var keyPresses = keypressline.Split('#');
        for (var i = 0; i < keyPresses.Length; i++)
        {               
            mySqlCommand.Parameters.AddWithValue("@Q" + i, keyPresses[i]);
        }

I've purposely left @Q as a string. If you cast to an Integer you will lose the fact if 0 is pressed first.

我故意把@Q作为字符串。如果您对一个整数进行强制转换,那么如果首先按下0,您将失去这个事实。

EDIT : Included load file code

编辑:包括加载文件代码

string fullPathAndFileName = @"C:\blah\blah.txt";
string keypresses;
using (StreamReader sr = new StreamReader(fullPathAndFileName)) 
{
    while (sr.Peek() >= 0) 
    {
        keyPresses = sr.ReadLine();
    ...
    }
}

#1


3  

I've assumed that your text file looks a little something like this, all on one line.

我假设你的文本文件看起来像这样,都在一行上。

100#400#900#

100 # 400 # 900 #

I also assume that you've read this line back into a variable of somename.

我还假设你已经把这一行读回到somename的变量中。

so

所以

        var keypressline = "100#400#900#";
        var keyPresses = keypressline.Split('#');
        for (var i = 0; i < keyPresses.Length; i++)
        {               
            mySqlCommand.Parameters.AddWithValue("@Q" + i, keyPresses[i]);
        }

I've purposely left @Q as a string. If you cast to an Integer you will lose the fact if 0 is pressed first.

我故意把@Q作为字符串。如果您对一个整数进行强制转换,那么如果首先按下0,您将失去这个事实。

EDIT : Included load file code

编辑:包括加载文件代码

string fullPathAndFileName = @"C:\blah\blah.txt";
string keypresses;
using (StreamReader sr = new StreamReader(fullPathAndFileName)) 
{
    while (sr.Peek() >= 0) 
    {
        keyPresses = sr.ReadLine();
    ...
    }
}