[英]Issue connecting to the SQL server from Web API
I have the below Controller that gets the account as the input parameter, which connects to the Sql server and will have to call the stored procedure passing account.The stored procedure inserts a new record if the Account is not present and updates it when already in there 我在下面的控制器中将帐户作为输入参数,该控制器连接到Sql Server,将必须调用传递过程的传递帐户。如果不存在该帐户,则存储过程将插入一条新记录,并在已经存在时对其进行更新那里
string strcon = ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString;
SqlConnection DbConnection = new SqlConnection(strcon);
DbConnection.Open();
SqlCommand command = new SqlCommand("[dbo].[usp_InserUpadte]", DbConnection);
command.CommandType = CommandType.StoredProcedure;
//create type table
DataTable table = new DataTable();
table.Columns.Add("AccountID", typeof(string));
table.Rows.Add(Account);
SqlParameter parameter = command.Parameters.AddWithValue("@Account_TT", table);
parameter.SqlDbType = SqlDbType.Structured;
parameter.TypeName = "Account_TT";
Below is the ConnectionString in web.config 以下是web.config中的ConnectionString
<connectionStrings>
<add name="DBConnection"
providerName="System.Data.SqlClient"
connectionString="Data Source=ServerName;Initial Catalog=DatabaseName;Integrated Security=True;MultipleActiveResultSets=True" />
</connectionStrings>
The stored procedure is like 存储过程就像
ALTER PROCEDURE [dbo].[usp_InserUpadte]
@account_TT AS account_TT READONLY
AS
BEGIN
SET NOCOUNT ON;
BEGIN TRANSACTION;
MERGE dbo.[Account] prj
USING @account_TT tt
ON prj.AccountID = tt.AccountID
WHEN MATCHED THEN UPDATE SET prj.CounterSeq = prj.CounterSeq+1
WHEN NOT MATCHED THEN INSERT (AccountID,CounterSeq)
VALUES (tt.AccountID, 1);
COMMIT TRANSACTION;
END;
where the table type is created with 在其中创建表类型的位置
CREATE TYPE account_TT AS TABLE
(
AccountID nvarchar(50),
)
GO
When I try to call the API it doesnot throw any exception but neither creates/update any records with the stored procedure. 当我尝试调用API时,它不会引发任何异常,但不会使用存储过程创建/更新任何记录。 I tried to debug the adding breakpoints.
我试图调试添加的断点。 I see the in
我看到了
DbConnection.Open();
Looks like the connection is not opened.
看起来连接没有打开。 I am able to connect to the SQL server from the same server I am working on though SQL Management Studio.
我可以通过SQL Management Studio从正在使用的同一台服务器连接到SQL Server。 Can anyone please suggest me what could be the issue.
谁能建议我可能是什么问题。
You probably have open connection.Correct way of opening connection 您可能已打开连接。正确的打开连接方式
string strcon = ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString;
using(SqlConnection dbConnection = new SqlConnection(strcon))
{
if (dbConnection.State==ConnectionState.Closed)
{
con.Open();
}
}
you didn't connect to data base correct. 您没有正确连接数据库。 try this:
尝试这个:
using (var connection = new SqlConnection(ConnectionString))
using (var command = new SqlCommand(commandText, connection) { CommandTimeout = 160, CommandType = commandType })
using (var dataAdaptor = new SqlDataAdapter(command))
{
command.Parameters.AddRange(parameters);
connection.Open();
dataAdaptor.Fill(dS);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.