3个回答
展开全部
假设button的id为button1,textbox的id为textbox1,如果你不分层的话,可以用下边的代码直接实现
protected void Button1_Click(object sender, EventArgs e)
{
string value = this.TextBox1.Text;
SqlConnection conn = new SqlConnection("server=.;database=数据库名;uid=你的登录名;pwd=你的登陆密码");
SqlCommand cmd = new SqlCommand("insert into 数据库表名(字段名) values ('"+value+"')");
cmd.ExecuteNonQuery();
conn.Close();
}
protected void Button1_Click(object sender, EventArgs e)
{
string value = this.TextBox1.Text;
SqlConnection conn = new SqlConnection("server=.;database=数据库名;uid=你的登录名;pwd=你的登陆密码");
SqlCommand cmd = new SqlCommand("insert into 数据库表名(字段名) values ('"+value+"')");
cmd.ExecuteNonQuery();
conn.Close();
}
展开全部
button事件中
string value=this.Textbox1.Text;
//执行数据库操作
// insert table value('"+value+"')
string value=this.Textbox1.Text;
//执行数据库操作
// insert table value('"+value+"')
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
private int addQuestion() {
int result = 0;
string sql = string.Format(
"INSERT INTO Question values('{0}','{1}',{2},'{3}','{4}','{5}','{6}','{7}')",
txtQuestion.Text.Trim(), answer, difficulty, subjectId,txtOptionA.Text.Trim(),
txtOptionB.Text.Trim(), txtOptionC.Text.Trim(),
txtOptionD.Text.Trim());
try
{
SqlCommand command = new SqlCommand(sql, DBHelper.connection);
DBHelper.connection.Open();
result = command.ExecuteNonQuery();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally {
DBHelper.connection.Close();
}
if (result == 1)
{
MessageBox.Show("增加问题成功!");
}
else {
MessageBox.Show("增加问题失败!");
}
txtQuestion.Text = "";
txtOptionA.Text = "";
txtOptionB.Text = "";
txtOptionC.Text = "";
txtOptionD.Text = "";
return result;
}
class DBHelper
{
private static string conSQL = "Data Source=.;Initial Catalog=MySchool;User ID=sa;Pwd=sasa";
public static SqlConnection connection = new SqlConnection(conSQL);
}
有问题联系我
int result = 0;
string sql = string.Format(
"INSERT INTO Question values('{0}','{1}',{2},'{3}','{4}','{5}','{6}','{7}')",
txtQuestion.Text.Trim(), answer, difficulty, subjectId,txtOptionA.Text.Trim(),
txtOptionB.Text.Trim(), txtOptionC.Text.Trim(),
txtOptionD.Text.Trim());
try
{
SqlCommand command = new SqlCommand(sql, DBHelper.connection);
DBHelper.connection.Open();
result = command.ExecuteNonQuery();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally {
DBHelper.connection.Close();
}
if (result == 1)
{
MessageBox.Show("增加问题成功!");
}
else {
MessageBox.Show("增加问题失败!");
}
txtQuestion.Text = "";
txtOptionA.Text = "";
txtOptionB.Text = "";
txtOptionC.Text = "";
txtOptionD.Text = "";
return result;
}
class DBHelper
{
private static string conSQL = "Data Source=.;Initial Catalog=MySchool;User ID=sa;Pwd=sasa";
public static SqlConnection connection = new SqlConnection(conSQL);
}
有问题联系我
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询