已有56人关注
提示格式不对
发表在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();//关闭数据连接
            }
        }
    }
}
科老师你好在给我看看,谢谢
分享到:
精彩评论 1
小科_mrkj
学分:43 LV2
2015-05-28
沙发
读者朋友:
    您好,在代码中插入断点,然后通过“添加监视”把SQL语句复制出来,拷到SQL Server管理器中执行,调试SQL语句。
首页上一页 1 下一页尾页 1 条记录 1/1页
手机同步功能介绍
友情提示:以下图书配套资源能够实现手机同步功能
明日微信公众号
明日之星 明日之星编程特训营
客服热线(每日9:00-17:00)
400 675 1066
mingrisoft@mingrisoft.com
吉林省明日科技有限公司Copyright ©2007-2022,mingrisoft.com, All Rights Reserved长春市北湖科技开发区盛北大街3333号长春北湖科技园项目一期A10号楼四、五层
吉ICP备10002740号-2吉公网安备22010202000132经营性网站备案信息 营业执照