开发者

passing variable to a stored procedure, c#

开发者 https://www.devze.com 2023-03-12 22:54 出处:网络
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:

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:

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

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

So, my first question is how do I pass it to the sproc, the code 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 keypress is 500#, and the second keypress is 300#. it should look like @Q1 = 500#, and @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.

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 #.

Any thoughts, suggestion, or documentation is appreciated.


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

100#400#900#

I also assume that you've read this line back into a variable of 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.

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();
    ...
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号