我想使用“MyPlaces”功能。我应该使用什么而不是commandType.StoredProcedure
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string connStr = ConfigurationManager.ConnectionStrings["MyDbConn"].ToString();
SqlConnection conn = new SqlConnection(connStr);
SqlCommand cmd = new SqlCommand("MyPlaces", conn);
cmd.CommandType = CommandType.StoredProcedure; //This part gives me an error
string email = Session["oldemailuser"].ToString();
cmd.Parameters.Add(new SqlParameter("@email", email));
conn.Open();
SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
string s = "<br />";
while (rdr.Read())
{
string name = s + " " + rdr.GetString(rdr.GetOrdinal("name"))
+ " located in ";
string location = rdr.GetString(rdr.GetOrdinal("location"))
+ "  ";
Label lbl_name = new Label();
lbl_name.Text = email;
form1.Controls.Add(lbl_name);
Label lbl_location = new Label();
lbl_location.Text = email;
form1.Controls.Add(lbl_location);
}
}
答案 0 :(得分:0)
希望你的sql函数名是“Myplaces”。如果是,那么用这个修改你的代码:
string email = Session["oldemailuser"].ToString();
SqlConnection conn = new SqlConnection(connStr);
SqlCommand cmd = new SqlCommand(conn);
cmd.CommandText = "SELECT MyPlaces(@email)";
cmd.CommandType = CommandType.Text;
cmd.Parameters.Add(new SqlParameter("@email", email));
conn.Open();