是此ErrMsg System.Data.SqlClient.SqlException(0x80131904)的SQL连接路径

时间:2017-04-30 09:33:25

标签: sql-server-2008 ado.net

新手在这里。

已经有一段时间了,不记得SQL Server 2008/2008 R2的整个安装过程。

这些是我在C:驱动器上找到的实例:

  • Program Files(x86)中,我有

    • MSSQL10_50.DEVSQLEXPRESS
  • Program files中,我有

    • MSSQL10.DEVSQLEXPRESS2
    • MSSQL10.MSSQLSERVER

我使用SSMS将SQL Server实例与Windows身份验证连接

不知何故,DEVSQLEXPRESS没有出现在SSMS中(来自SQL Server 2008 R2),我无法删除它。

DEVSQLEXPRESS2可以在SSMS中连接。现在我正在使用DEVSQLEXPRESS2进行Asp.net Web Api测试。

这是我的代码。没有编译错误。

public User Add(User contact)
{
    try
    {               
        SqlConnection conn = new SqlConnection("Data Source= .\\DEVSQLEXPRESS2;Initial Catalog=MyBookingHub;Integrated Security=true");

        SqlCommand sqlCmd = new SqlCommand();
        sqlCmd.CommandType = CommandType.Text;
        sqlCmd.CommandText = "INSERT INTO Users (UserName,UserPassword,UserEmailAddr,UserMobile) 
Values (@UserName,@UserPassword,@UserEmailAddr,@UserMobile)";

        sqlCmd.Connection = conn;

        sqlCmd.Parameters.AddWithValue("@UserName", contact.UserName);
        sqlCmd.Parameters.AddWithValue("@UserPassword", contact.UserPassword);
        sqlCmd.Parameters.AddWithValue("@UserEmailAddr", contact.UserEmailAddr);
        sqlCmd.Parameters.AddWithValue("@UserMobileNo", contact.UserMobileNo);

         conn.Open();

         int rowInserted = sqlCmd.ExecuteNonQuery();

         conn.Close();

         var ctc = new User()
         {
             UserId = rowInserted,
             UserName = contact.UserName,
             UserPassword = contact.UserPassword,
             UserEmailAddr =contact.UserEmailAddr,
             UserMobileNo = contact.UserMobileNo
          };

          return ctc;
      }
      catch (Exception ex)
      {
          string err = ex.ToString();
          return null;   
      }
 }

遇到这个问题:

  

System.Data.SqlClient.SqlException(0x80131904):''附近的语法不正确。

     

at System.Data.SqlClient.SqlConnection.OnError(SqlException exception,Boolean breakConnection,Action 1 wrapCloseInAction)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action
1 wrapCloseInAction)
  在System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj,Boolean callerHasConnectionLock,Boolean asyncClose)
  在System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior,SqlCommand cmdHandler,SqlDataReader dataStream,BulkCopySimpleResultSet bulkCopyHandler,TdsParserStateObject stateObj,Boolean& dataReady)   在System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds,RunBehavior runBehavior,String resetOptionsString,Boolean isInternal,Boolean forDescribeParameterEncryption)
  在System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(的CommandBehavior cmdBehavior,RunBehavior runBehavior,布尔returnStream,布尔异步,的Int32超时,任务&安培;任务,布尔asyncWrite,布尔inRetry,SqlDataReader的DS,布尔describeParameterEncryptionRequest)
  在System.Data.SqlClient.SqlCommand.RunExecuteReader(的CommandBehavior cmdBehavior,RunBehavior runBehavior,布尔returnStream,字符串方法,TaskCompletionSource 1 completion, Int32 timeout, Task& task, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry)
at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource
1次完成,字符串方法名,布尔sendToPipe,的Int32超时,布尔&安培; usedCache,布尔asyncWrite,布尔inRetry )
  在System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
  at MyBookingService.DataAccessModelRepository.UserRepository.Add(用户联系人)在C:\ Users \ Edward \ Documents \ Visual Studio 2015__AspNet_Web_Api \ MyBookingService \ MyBookingService \ DataAccessModelRepository \ UserRepository.cs:第82行

     

ClientConnectionId:65379b7b-f681-459c-b98f-99e55d09fdcc
  错误号码:102,状态:1,等级:15

更新

SQL Server中的表:

UserId        int
UserName      nvarchar(30)
UserPassword  nvarchar(30)
UserEmailAddr nvarchar(120)
UserMobileNo  nvarchar(20)
CreatedOn     datetime     //Allow Null

//- use SqlDbType
sqlCmd.Parameters.Add("@UserName", SqlDbType.NVarChar, 20).Value = contact.UserName;
sqlCmd.Parameters.Add("@UserPassword", SqlDbType.NVarChar, 30).Value = contact.UserPassword;

sqlCmd.Parameters.Add("@UserEmailAddr", SqlDbType.NVarChar, 120).Value = contact.UserEmailAddr;

sqlCmd.Parameters.Add("@UserMobileNo", SqlDbType.NVarChar, 20).Value = contact.UserMobileNo;

结果仍然与之前相同

0 个答案:

没有答案