【发布时间】:2015-01-07 21:00:33
【问题描述】:
尝试创建一个用于在家学习目的的 asp.net c# 表单,但我绝对难以连接到我的存储过程。
我肯定在正确的数据库中,因为当我通过 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 文章。您可能需要向下滚动。 Using Parameters With a SqlCommand and a Stored Procedure
-
没有 SQL Server 2013 版本.... 我们有 SQL Server 2012 和 2014 .... 你用的是哪一个?
-
对不起 - 它的视觉表达 2013 在里面使用 sql server - 如果可能的话我会修改标题。
-
你遇到什么样的错误?
-
找不到存储过程“InsertEnquiry”。
标签: c# asp.net sql-server stored-procedures ado.net