试图创建一个asp.net c#表格用于学习目的在家里,我绝对努力连接到我的storedprocedure。存储过程中的SQL Server和使用存储过程
我肯定是在正确的数据库中,因为当我通过cmdprompt启动数据库并通过数据源连接到它时,它在视觉设计中连接并查找存储过程。所以一定有我做错的事情?我一直在Google上搜索大约30-40分钟,我尝试过的所有内容都没有解决我的问题。有什么建议吗?
const string constring = @"Data Source=(localdb)\ProjectsV12;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False";
public static int InsertEnquiry(string Name, string Subject, string Email, string Message, string Phone) {
//default ticketid of 0 which will be changed to -1 if an error or greater than 0 if successful
int TicketID = 0;
if (String.IsNullOrEmpty(Phone)) Phone = "0";
using (var conn = new SqlConnection(constring)) {
//create command
SqlCommand command = new SqlCommand();
//tell the command which connection its using
command.Connection = conn;
//inform command that it is a stored procedure and the name of stored procedure
command.CommandText = "InsertEnquiry";
command.CommandType = CommandType.StoredProcedure;
//add the parameters to the sqlcommand
command.Parameters.Add(new SqlParameter("@Name", SqlDbType.NVarChar)).Value = Name;
command.Parameters.Add(new SqlParameter("@Subject", SqlDbType.NVarChar)).Value = Subject;
command.Parameters.Add(new SqlParameter("@Phone", SqlDbType.NVarChar)).Value = Phone;
command.Parameters.Add(new SqlParameter("@Email", SqlDbType.NVarChar)).Value = Email;
command.Parameters.Add(new SqlParameter("@Message", SqlDbType.NVarChar)).Value = Message;
// try run command and set TicketID to the row inserted into the table.
try {
conn.Open();
//run command
command.ExecuteNonQuery();
//return scope identity of row.
TicketID = (int)command.Parameters["@TicketID"].Value;
}
catch (Exception e) {
//show -1 to state there is an error
TicketID = -1;
}
}
return TicketID;
}
}
这里有一个MSDN文章。您可能需要向下滚动。 [使用带有SqlCommand和存储过程的参数](http://msdn.microsoft.com/zh-cn/library/yy6y35y8(v = vs.110).aspx) – Cameron
没有SQL Server 2013版本...我们有SQL Server 2012和2014 ....你使用哪一个? –
对不起 - 它的视觉快速2013年使用SQL服务器内 - 我会修改标题如果可能的话。 – cooLLedge