【发布时间】:2015-10-23 12:56:10
【问题描述】:
我有一个asp.net 应用程序,它是地点的随机生成器。
目前我的值位于我的代码后面,但我想将这些值移动到我的 SQL Server 数据库中,但我不知道如何执行此操作。作为参考,我使用的是 SQL Server Management Studio。
这是可能的还是我把它复杂化了?
代码背后
protected void BtnDecideForMe_Click(object sender, EventArgs e)
{
List<string> Eat = new List<string>();
Eat.Add("Chinese Buffet");
Eat.Add("Harvester");
Eat.Add("Frankie & Benny's");
Eat.Add("Hungry Horse");
Eat.Add("Blaize");
Eat.Add("Chiquito");
Eat.Add("Cafe Football");
Eat.Add("Nando's");
Random Place = new Random();
int Places = Place.Next(0, Eat.Count);
txtDestination.Text = Eat[Places];
}
查看
<div class="row">
<div class="col-sm-3">
<asp:TextBox class="form-control" ID="txtDestination" runat="server" disabled></asp:TextBox>
</div>
<div class="col-sm-2">
<asp:Button class="btn btn-success" ID="BtnDecideForMe" runat="server" Text="Decide For Me" OnClick="BtnDecideForMe_Click" />
</div>
</div>
提供建议但无法正常工作的代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
namespace PaydayLunchGenerator
{
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void BtnDecideForMe_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString =
"Data Source=DEV-116\\ONLINE;" +
"Initial Catalog=PaydayLunch;" +
"Integrated Security=True;";
conn.Open();
//using (SqlConnection conn = new SqlConnection(PaydayLunchConnectionString1))
using (SqlCommand cmd = new SqlCommand("dbo.GetRandomPlace", conn))
{
cmd.CommandType = CommandType.StoredProcedure;
// set up the parameters
cmd.Parameters.Add("@OutputVar", SqlDbType.VarChar).Direction = ParameterDirection.Output;
// open connection and execute stored procedure
conn.Open();
cmd.ExecuteNonQuery();
// read output value from @OutputVar
string place = Convert.ToString(cmd.Parameters["@OutputVar"].Value);
conn.Close();
txtDestination.Text = place;
}
}
}
}
【问题讨论】:
-
大概有多少个值?
-
@Alec 我帖子中的所有内容以及其他任何人建议的内容,因此此列表可能会增加。我确实还有另外 8 个要添加到我的列表中,但还没有
-
我添加了一个答案来创建一个表格并为你插入这些东西。
标签: c# asp.net sql-server