我正在使用数据库进行作业,并且正在检查是否有任何过期电影。我想知道是否有可能从数据库表中获取一个值并将其分配给一个字符串变量。这是我的代码
<asp:Panel runat="server" ID="CheckoutPanel" Visible="false">
<h4>Movies that are Checked Out</h4>
<asp:GridView id="one_data2" AutoGenerateColumns="false" runat="server">
<Columns>
<asp:BoundField DataField="MovieID"
HeaderText="Movie ID"/>
<asp:BoundField DataField="SubscriberID"
HeaderText="Checked Out On"/>
<asp:BoundField DataField="DueDate"
HeaderText="Due On" />
</Columns>
</asp:GridView>
</asp:Panel>
上面是我想从中获取信息的面板。下面是我制作的代码,但不太好用。
protected void DueMovies(Object src, EventArgs e)
{
get_connection();
connection.Open();
command = new SqlCommand("SELECT * FROM checkout", connection);
reader = command.ExecuteReader();
int movieId = one_data2.Rows["0"]["MovieID"].ToString();
string subId = one_data2.Rows["1"]["SubscriberID"].ToString();
string dueDate = one_data2.Rows["2"]["DueDate"].ToString();
DateTime lateDate = Convert.ToDateTime(one_data2.Rows["2"]["DueDate"]);
if(DateTime.Now >= lateDate)
{
string EmailMsg = "A late message has been sent to " + subId;
}
else
{
lblInfo.Text = "No movies are due soon";
}
}
变量不起作用,因为我收到一条错误消息,说我无法从 int 转换为字符串。我试图设置为 int 但这也不起作用。有两个到期日变量,但我正在测试哪个有效。
沿着这条线尝试一些东西 - 提供您自己的方法来创建连接。
static void Main(string[] args)
{
string someText;
using (var connection = new SqlConnection("..."))
{
// autodisposing if no longer needed
using (var command = new SqlCommand("SELECT * FROM checkout"))
{
var msg = new List<string>(); // collects msg to avoid several updates to label
// autodisposing ifnolonger needed
using (var reader = command.ExecuteReader())
{
if (reader.HasRows)
{
// if rows present, read one by one until done
while(reader.NextResult())
{
// process one row
var movieId = Convert.ToInt32(reader["MovieId"]);
var subId = Convert.ToInt32(reader["SubscriberID"]);
var dueDate = Convert.ToDateTime(reader["DueDate"]);
// check due
if (DateTime.Now > dueDate)
msg.Add(SendLateMail(subId)); // send mail and store message
}
// make label text by joining all msg with newlines
someText = string.Join("\n", msg);
}
else
{
// no rows found, set label text
someText = "No movies due.";
}
// TODO: set your labels .Text
}
}
}
}
static string SendLateMail(int subId)
{
// Todo: send mail
// create text to accumulate for label
return $"Due Email send to {subId}";
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句