简体   繁体   English

尝试连接到SQL Server数据库时出现“ SqlException未处理”错误

[英]'SqlException was unhandled' error when trying to connect to SQL Server database

I have a program that takes information passed to it, and puts the information in a database. 我有一个程序,它接收传递给它的信息,并将信息放入数据库中。 The program is being created in VS2008 for a Windows Ce 6.0 device, and I am having trouble connecting to the database. 该程序是在VS2008中为Windows Ce 6.0设备创建的,无法连接到数据库。 I want to connect over the local network, but not entirely too sure how to do so. 我想通过本地网络进行连接,但并不十分确定如何进行连接。 Here is my code for the function that calls the database. 这是我调用数据库的函数的代码。

public void callSQL(string partNumber, int total, int numOfPacks, string dunsNumber, string serialNumber, string laneNumber)
{
    //establish connection
    string conString = "Data Source=10.97.2.67,1433;Initial Catalog=testing.mdf;User ID=**********;Persist Security Info=false";

    using (SqlConnection con = new System.Data.SqlClient.SqlConnection(conString))
    {
        con.Open();

        try
        {
            //open the server
            //Insert values passed into the method
            SqlCommand myCommand = new SqlCommand("INSERT INTO test (Part_Number, total, number_of_packs, dunsNumber, serialNumber, truck_number) VALUES (@Part_Number,@total,@number_of_packs,@dunsNumber,@serialNumber,@truck_number)", con);
            myCommand.Parameters.AddWithValue("@Part_Number", partNumber);
            myCommand.Parameters.AddWithValue("@total", total);
            myCommand.Parameters.AddWithValue("@number_of_packs", numOfPacks);
            myCommand.Parameters.AddWithValue("@dunsNumber", dunsNumber);
            myCommand.Parameters.AddWithValue("@serialNumber", serialNumber);
            myCommand.Parameters.AddWithValue("@truck_number", laneNumber);

            //execute the query
            myCommand.ExecuteNonQuery();
            con.Close();
        }
        catch (SqlException e)
        {
           string error = e.ToString();
           error = error.ToLower();

           if (error.Contains("duplicate") == true)
           {
              Console.WriteLine("You have already scanned this barcode.");
           }
        }
    }
}

When I run the code, the code stops at con.Open(); 当我运行代码时,代码在con.Open()处停止; and I get an error which says 我得到一个错误,说

SqlException was unhandled SqlException未处理

Here is the information from the error: 这是来自错误的信息:

System.Data.SqlClient.SqlException {"SqlException"} System.Data.SqlClient.SqlException {“ SqlException”}
[System.Data.SqlClient.SqlException] {"SqlException"} [System.Data.SqlClient.SqlException] {“ SqlException”}
{Systen.Data.SqlClient.SqlErrorCOllection} {Systen.Data.SqlClient.SqlErrorCOllection}
LineNumber 0 行号0
Message "SqlException" 消息“ SqlException”
Number 17 17号
Procedure "ConnectionOpen(Connect())." 过程“ ConnectionOpen(Connect())。”
Server "10.97.2.67,1433" 服务器“ 10.97.2.67,1433”
Source ".Net SqlClient Data Provider" 源“ .Net SqlClient数据提供程序”
StackTrace 堆栈跟踪
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, TdsParserState state) 在System.Data.SqlClient.SqlConnection.OnError(SqlException异常,TdsParserState状态)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, TdsParserState state) 在System.Data.SqlClient.SqlInternalConnection.OnError(SqlException异常,TdsParserState状态)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning() 在System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()
at System.Data.SqlClient.TdsParser.Connect(String host, SqlInternalConnection connHandler, Int32 timeout) 在System.Data.SqlClient.TdsParser.Connect(字符串主机,SqlInternalConnection connHandler,Int32超时)
at System.Data.SqlClient.SqlInternalConnection.OpenAndLogin() 在System.Data.SqlClient.SqlInternalConnection.OpenAndLogin()
at System.Data.SqlClient.SqlInternalConnection..ctor(SqlConnection connection, Hashtable connectionOptions) 在System.Data.SqlClient.SqlInternalConnection..ctor(SqlConnection连接,Hashtable connectionOptions)
at System.Data.SqlClient.SqlConnection.Open() 在System.Data.SqlClient.SqlConnection.Open()
at scan.partObject.callSQL(String partNumber, Int32 total, Int32 numOfPacks, String dunsNumber, String serialNumber, String laneNumber) 在scan.partObject.callSQL(字符串partNumber,Int32总计,Int32 numOfPacks,字符串dunsNumber,字符串serialNumber,字符串laneNumber)
at scan.Program.Main(String[] args) 在scan.Program.Main(String [] args)

Thanks 谢谢

To me it looks like your IP Address issue. 在我看来,这似乎是您的IP地址问题。 Check your IP Address and try connecting it again. 检查您的IP地址,然后尝试重新连接。

IPv4 Address should have 4 domain each between 0 to 255. For details visit here IPv4地址应具有4个域,每个域介于0到255之间。有关详细信息,请访问此处

Your IP Address is "10.97.2.67,1433", I think remove 1433 and then try again. 您的IP地址为“ 10.97.2.67,1433”,我想删除1433,然后重试。 In order to use port number with your IP Address, change your IP Address as "10.97.2.67:1433". 为了将端口号与IP地址一起使用,请将IP地址更改为“ 10.97.2.67:1433”。 Hopefully this will solve your issue. 希望这能解决您的问题。

This was an error due to the connection string. 由于连接字符串,这是一个错误。 Here is the format the connection string should be if you want to connect to a SQL server over a network. 如果要通过网络连接到SQL Server,则连接字符串应采用以下格式。

Data Source=myServerAddress;Initial Catalog=myDataBase; Integrated Security=SSPI;User ID=myDomain\myUsername;Password=myPassword;

myServerAddress can be found on SQL properties. 可以在SQL属性上找到myServerAddress。 Good Luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试连接到SQL Server数据库时出错 - Error when trying to connect to SQL Server database 错误-尝试连接到SQL Server数据库时出现“找不到或无法访问服务器” - Error - “The server was not found or was not accessible” when trying to connect to sql server database 尝试在SQL Server 2008 R2上调用某个列时,在C#上未处理SqlException - SqlException unhandled on C# when trying to call a certain column on SQL Server 2008 R2 尝试连接到数据库EF和SQL Server时出错 - Error when trying to connect to the database EF and SQL Server 尝试连接时出现SQL Server错误 - SQL Server error when trying to connect Catch SqlException尝试通过按钮单击事件向sql数据库添加数据时抛出错误无效语法 - Catch SqlException throwing error invalid syntax when trying to add data to sql database through button click event 尝试连接到SQL Server数据库时出现错误 - Getting Error While trying to connect to a SQL Server database 尝试连接到SQL数据库时出现权限错误 - Getting a permission error when trying to connect to sql database 尝试从远程计算机上的程序连接到SQL Server时出错 - Error when trying to connect to SQL Server from program on remote computer 数据库未启动但 SQL Server 已准备好连接时的 SqlException - SqlException when the database is not started but SQL Server is ready for connections
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM