在我的约会表中,我有aDate,aTime,aContact,aHeight,aWeight,patientID,mcID和aStatus字段。 aDate,aTime,aContact,aHeight,aWeight,patientID,mcID值将通过文本框输入并插入到数据库中,但只有aStatus字段我希望它自动将这个名为(等待)aStatus字段的值插入约会表中单击按钮,如何为aStatus执行此操作?
protected void btnCreate_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["sacpConnectionString"].ConnectionString))
{
try
{
SqlCommand cmd = new SqlCommand();
Guid guid;
guid = Guid.NewGuid();
string sql = "INSERT INTO appointment (aDate, aTime, aContact, aHeight, aWeight, patientID, mcID)";
sql += "VALUES (@aDate, @aTime, @aContact, @aHeight, @aWeight, @patientID, @mcID)";
cmd.Parameters.AddWithValue("@aDate", txtDate.Value);
cmd.Parameters.AddWithValue("@aTime", txtTime.Value);
cmd.Parameters.AddWithValue("@aContact", txtContact.Value.Trim());
cmd.Parameters.AddWithValue("@aHeight", txtHeight.Value.Trim());
cmd.Parameters.AddWithValue("@aWeight", txtWeight.Value.Trim());
cmd.Parameters.AddWithValue("@patientID", txtpatientID.Value.Trim());
cmd.Parameters.AddWithValue("@mcID", txtmcID.Value.Trim());
cmd.Connection = con;
cmd.CommandText = sql;
con.Open();
cmd.ExecuteNonQuery();
// Session.Add("Username", txtFirstName.Value);
// Session.Add("Password", txtContact.Value);
// FormsAuthentication.SetAuthCookie(txtFirstName.Value, true);
Response.Redirect("../index.aspx");
}
finally
{
con.Close();
}
}
}
}
答案 0 :(得分:1)
如果你想插入一些integer
值(例如:ID
),每个新记录都会增加,你可以使用IDENTITY
列,但在这种情况下,你要插入每条记录的默认string
值,以便您可以在INSERT INTO
语句中直接指定它。
试试这个:
String strStatus="waiting";
string sql = "INSERT INTO appointment (aStatus,aDate, aTime, aContact, aHeight, aWeight, patientID, mcID)";
sql += "VALUES (@aStatus,@aDate, @aTime, @aContact, @aHeight, @aWeight, @patientID, @mcID)";
cmd.Parameters.AddWithValue("@aStatus", strStatus);
cmd.Parameters.AddWithValue("@aDate", txtDate.Value);
cmd.Parameters.AddWithValue("@aTime", txtTime.Value);
cmd.Parameters.AddWithValue("@aContact", txtContact.Value.Trim());
cmd.Parameters.AddWithValue("@aHeight", txtHeight.Value.Trim());
cmd.Parameters.AddWithValue("@aWeight", txtWeight.Value.Trim());
cmd.Parameters.AddWithValue("@patientID", txtpatientID.Value.Trim());
cmd.Parameters.AddWithValue("@mcID", txtmcID.Value.Trim());
答案 1 :(得分:0)
您可以尝试通过执行以下查询来设置数据库中列的默认值:
ALTER TABLE appointment
ALTER aStatus SET DEFAULT '(waiting)'
答案 2 :(得分:0)
SQL字符串
string sql = "INSERT INTO appointment (aDate, aTime, aContact, aHeight, aWeight, patientID, mcID, aStatus)";
你的价值观
sql += "VALUES (@aDate, @aTime, @aContact, @aHeight, @aWeight, @patientID, @mcID, 'waiting')";
答案 3 :(得分:0)
这是解决方案,如Sudhakar Tillapudi,但没有添加参数:
String strStatus="waiting";
string sql = "INSERT INTO appointment (aStatus,aDate, aTime, aContact, aHeight, aWeight, patientID, mcID)";
sql += "VALUES ('waiting',@aDate, @aTime, @aContact, @aHeight, @aWeight, @patientID, @mcID)";
cmd.Parameters.AddWithValue("@aDate", txtDate.Value);
cmd.Parameters.AddWithValue("@aTime", txtTime.Value);
cmd.Parameters.AddWithValue("@aContact", txtContact.Value.Trim());
cmd.Parameters.AddWithValue("@aHeight", txtHeight.Value.Trim());
cmd.Parameters.AddWithValue("@aWeight", txtWeight.Value.Trim());
cmd.Parameters.AddWithValue("@patientID", txtpatientID.Value.Trim());
cmd.Parameters.AddWithValue("@mcID", txtmcID.Value.Trim());