Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.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#SQL连接字符串_C#_Sql_Connection String - Fatal编程技术网

使用文本框值的C#SQL连接字符串

使用文本框值的C#SQL连接字符串,c#,sql,connection-string,C#,Sql,Connection String,我无法调用文本框的值作为IP地址 private void IDSearch() { DataTable dt = new DataTable(); SqlConnection sqlConnection2 = new SqlConnection(@"Server='" + txtIPReal.Text + "';Database=xstore;User Id=user;Password=pass;"); cmd3 = sqlConnection2.Cr

我无法调用
文本框的值作为IP地址

private void IDSearch() {
    DataTable dt = new DataTable();

    SqlConnection sqlConnection2 = new SqlConnection(@"Server='" + txtIPReal.Text +
        "';Database=xstore;User Id=user;Password=pass;");
    cmd3 = sqlConnection2.CreateCommand();
    cmd4 = sqlConnection2.CreateCommand();

    try {
        sqlConnection2.Open();

        // sql code here
    }
    catch (Exception) {
        // exception handling here
    }
}

它捕捉到一个错误。有人能帮我吗?

您的连接字符串应该是这样的

@"Data Source="+ txtIPReal.Text +";Integrated Security=False;User ID=user;Password=pass;Initial Catalog=xstore;"

您必须遵循连接字符串的正确格式

您能提供该错误吗捕获了什么错误/异常?它指向我的catchcatch{MessageBox.Show(“连接到数据库的错误\n\n请与HD1协调以连接到数据库”,“连接失败”},MessageBoxButtons.OK,MessageBoxIcon .Error);}您可能需要考虑手动地手工输入IP地址<代码> @“Server=192.1681.100;数据库=XSturst;用户ID =用户;密码= PASS;”< /C> >并检查您的工作是否首先复制错误。因为您的问题已经指出,发生这种情况是因为
txtIPReal.Text
没有足够的信息或工作来显示您自己的调试尝试。它可以工作:)但是可以有两个连接字符串?SqlConnection SqlConnection=new SqlConnection(@“Data Source=“+txtIPReal.Text+“Integrated Security=False;User ID=User;Password=pass;Initial Catalog=xstore;”;SqlConnection sqlConnection2=new SqlConnection(@“数据源=localhost;Integrated Security=False;用户ID=User;Password=pass;Initial Catalog=xstore;”);是的,如果单独保存,但每次必须设置一个连接字符串,请检查此项