Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从文件中给出存储过程的输入参数_C#_Sql Server_Stored Procedures - Fatal编程技术网

C# 从文件中给出存储过程的输入参数

C# 从文件中给出存储过程的输入参数,c#,sql-server,stored-procedures,C#,Sql Server,Stored Procedures,我有一个包含3个参数的存储过程,我认为我没有正确地使用存储过程的第一个参数 string path = @"D:\test2"; var ngd = Directory.EnumerateFiles(path, "*.txt").Select(file => File.ReadAllLines(file)).FirstOrDefault(); using (SqlConnection connection = new SqlConnection("Data Source=;Initial

我有一个包含3个参数的存储过程,我认为我没有正确地使用存储过程的第一个参数

string path = @"D:\test2";
var ngd = Directory.EnumerateFiles(path, "*.txt").Select(file => File.ReadAllLines(file)).FirstOrDefault();
using (SqlConnection connection = new SqlConnection("Data Source=;Initial Catalog=;User ID=;Password="))
{
    using (SqlCommand cmd = new SqlCommand("usp_SaveData", connection))
    {
        try
        {
            await Task.Delay(1500);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@request_xml", SqlDbType.NVarChar, 999999).Value = ngd;
            cmd.Parameters.Add("@ST", SqlDbType.NVarChar, 50).Value = MachineName;
            cmd.Parameters.Add("@SN", SqlDbType.NVarChar, 50).Value = listBoxItem;
            cmd.ExecuteNonQuery();
            MessageBox.Show("Good");
            connection.Open();
        }
        catch
        {
            MessageBox.Show("Verification failed!");
        }
    }
}
我用SQL运行了我的存储过程,一切正常,我运行了一个不同的过程,只需要
@ST
@SN
,它工作了,但现在在这个过程中,我给出了
请求xml

var ngd = Directory.EnumerateFiles(path, "*.txt").Select(file => File.ReadAllLines(file)).FirstOrDefault(); 
它不起作用了


我的问题是,我是否应该将文件中的数据以不同的方式存储在变量中?是否有“正确的方法”要将文件存储在可由输入参数使用的变量中?

问题在于您使用的是
ReadAllLines
,它返回
字符串
数组
,每个元素都是文件中的一行

因此,在下面的一行中,您实际上传递的是一个
string[]
,而不是
string

cmd.Parameters.Add("@request_xml", SqlDbType.NVarChar, 999999).Value = ngd;
var ngd = Directory.GetFiles(path, "*.txt").Select(file => File.ReadAllText(file)).FirstOrDefault();
您应该改用
ReadAllText
,它返回一个
字符串

cmd.Parameters.Add("@request_xml", SqlDbType.NVarChar, 999999).Value = ngd;
var ngd = Directory.GetFiles(path, "*.txt").Select(file => File.ReadAllText(file)).FirstOrDefault();

你能用sored程序更新你的问题吗?@DDDSoft-见我的答案谢谢你的解释:)