提示格式不对
发表在C#图书答疑
2015-05-28
是否精华
是
否
版块置顶:
是
否
小using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace AddData
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnSave_Click(object sender, EventArgs e)
{
string connstr = @"Data Source=ZHNDF9BTCCGAVKQ;Initial Catalog=MyDataBase;Integrated Security=True";//数据库连接字符串
SqlConnection con = new SqlConnection(connstr);//创建Connection对象
try
{
//插入数据的sql语句
string sql = string.Format("insert into 学生信息 values('{0}','{1}',{2},'{3}','{4}')", this.txtName.Text.Trim(), this.txtSex.Text.Trim(), Convert.ToInt32(this.txtAge.Text), this.txtClass.Text.Trim(), this.txtPhone.Text.ToString());
SqlCommand cmd = new SqlCommand(sql, con);//创建Command对象
con.Open();//打开数据库连接
cmd.ExecuteNonQuery();//执行sql语句
MessageBox.Show("数据保存成功");
}
catch (Exception ex)
{
throw new Exception(ex.Message);//异常处理
}
finally
{
con.Close();//关闭数据连接
}
}
}
}
科老师你好在给我看看,谢谢
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace AddData
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnSave_Click(object sender, EventArgs e)
{
string connstr = @"Data Source=ZHNDF9BTCCGAVKQ;Initial Catalog=MyDataBase;Integrated Security=True";//数据库连接字符串
SqlConnection con = new SqlConnection(connstr);//创建Connection对象
try
{
//插入数据的sql语句
string sql = string.Format("insert into 学生信息 values('{0}','{1}',{2},'{3}','{4}')", this.txtName.Text.Trim(), this.txtSex.Text.Trim(), Convert.ToInt32(this.txtAge.Text), this.txtClass.Text.Trim(), this.txtPhone.Text.ToString());
SqlCommand cmd = new SqlCommand(sql, con);//创建Command对象
con.Open();//打开数据库连接
cmd.ExecuteNonQuery();//执行sql语句
MessageBox.Show("数据保存成功");
}
catch (Exception ex)
{
throw new Exception(ex.Message);//异常处理
}
finally
{
con.Close();//关闭数据连接
}
}
}
}
科老师你好在给我看看,谢谢