Passing a variable to a stored procedure, C #

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

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

What I need to do is pass these keystrokes, when they enter, to sproc.

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

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

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

Do I need to convert the keystrokes to a string first? If so, what is the best way to do this, since I have about 10 questions that are given, so I have 10 unique keystrokes that return.

And secondly, since there is a # sign at the end of each keystroke, will the sproc input be nvarchar, not int? Or what is the best way to handle #.

Any thoughts, suggestions or documentation are appreciated.

+3
source share
1 answer

I assumed that your text file looks something like this: everything is on the same line.

100 # 400 # 900 #

I also assume that you read this line back in the somename variable.

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 intentionally left @Q as a string. If you click on Integer, you will lose the fact if you first press 0.

EDIT: Code of the downloaded download file

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