从字符串转换日期和/或时间时,转换失败

.Net技术 码拜 9年前 (2015-04-01) 2172次浏览 0个评论
 
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 学生成绩管理系统
{
    public partial class StudentMsgFrm : Form
    {
        private int current = 1;
        public StudentMsgFrm()
        {
            InitializeComponent();
           
        }

        private void btnYes_Load(object sender, EventArgs e)
        {
            rdoMale.Checked = true;//默认学生性别为男
            cboDept.SelectedIndex = 0;//初始选择组合框中的第一项(“计算机系”)
            dtBirthday.MaxDate = DateTime.Now;//设置出生日期的最大值为系统当前时间
            dtBirthday.Value = dtBirthday.MinDate;//设置出生日期默认值为最小值
            timeGo.Enabled = true;//启动计时器
        }

        private void cboDept_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (cboDept.SelectedIndex)
            {case 0:
                    lstSpec.Items.Clear();//
                    lstSpec.Items.Add("计算机科学与技术");
                    lstSpec.Items.Add("信息与计算科学");
                    break;
                case 1:
                     lstSpec.Items.Clear();
                    lstSpec.Items.Add("集成电路与集成系统");
                    lstSpec.Items.Add("集成电路设计");
                    break;
                case 2:
                    lstSpec.Items.Clear();
                    lstSpec.Items.Add("通信工程");
                    lstSpec.Items.Add("电子信息工程");
                    lstSpec.Items.Add("电磁场与无线技术");
                    lstSpec.Items.Add("机械制造及自动化");
                    break;
                case 3:
                    lstSpec.Items.Clear();
                    lstSpec.Items.Add("国际经济与贸易");
                    lstSpec.Items.Add("电子商务");
                    lstSpec.Items.Add("信息管理与信息系统");
                    lstSpec.Items.Add("财务管理");
                    break;
                default:
                    lstSpec.Items.Clear();
                    lstSpec.Items.Add("数字动画");
                    lstSpec.Items.Add("影视动画");
                    lstSpec.Items.Add("商务插画");
                    break;


            }
            lstSpec.SelectedIndex = 0;//设置默认专业为第一个选项
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string sex = "";
            if (rdoMale.Checked)
                sex = rdoMale.Text;
            else
                sex =rdoFemale.Text;
            string dept = cboDept.SelectedItem.ToString();
            string spec = lstSpec.SelectedItem.ToString();

            string hobby = "";
            if (checkBox1.Checked) hobby += checkBox1.Text;
            if (checkBox2.Checked) hobby +="、"+ checkBox2.Text;
            if (checkBox3.Checked) hobby += "、" + checkBox3.Text;
            if (checkBox4.Checked) hobby += "、" + checkBox4.Text;
            if (checkBox5.Checked) hobby += "、" + checkBox5.Text;
            if (checkBox6.Checked) hobby += "、" + checkBox6.Text;
            string sql = String.Format("INSERT INTO StudentMsg(StudentName,Sex,Birthday,Department,Speciality,Hobby)VALUES("{0}","{1}","{2}","{3}","{4}","{5}")", txtName.Text, sex, dtBirthday.Value, dept, spec, hobby);//sq;语句
            string connString = @"Data Source=Win-01412261307;Initial Catalog=MySchool;Integrated Security=True";
            using (SqlConnection conn = new SqlConnection(connString))
            {
                conn.Open();
                SqlCommand comm = new SqlCommand(sql, conn);//创建command对象
                int n = comm.ExecuteNonQuery();//执行“添加”命令,返回值为更新的行数
       
                if (n > 0)
                {
                    MessageBox.Show("添加学生信息成功", "添加成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("添加学生信息失败", "添加失败", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            } 
            /*string info = "你的姓名是:" + txtName.Text;
            info += "\n性别为:" + sex;
            info += "\n出生年月为:" + dtBirthday.Value.ToShortDateString();
            info += "\n您是:" + dept+"系"+spec+"专业学生";
            info += "\n你的兴趣有:" + hobby;

            MessageBox.Show(info, "学生信息",
                  MessageBoxButtons.OK, MessageBoxIcon.Information);
            */
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void timeGo_Tick(object sender, EventArgs e)
        {
            if (lblTip.Left >= this.Width)
            {
                lblTip.Left = 0;
            }
            lblTip.Left += 1;
        }

        }
    }

从字符串转换日期和/或时间时,转换失败
开始我以为是”{3}”是不是不要单引号,改了后又显示“星期二附近有错误”,大神们帮忙看看啊
从字符串转换日期和/或时间时,转换失败
35分
dtBirthday.Value取到的是DateTime,你在format里面设置下格式,比如{0:yyyy-MM-dd}
从字符串转换日期和/或时间时,转换失败
异常指向的是

 int n = comm.ExecuteNonQuery();//执行“添加”命令,返回值为更新的行数
从字符串转换日期和/或时间时,转换失败
5分
个人建议你最好用DbParameter的方式传递

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明从字符串转换日期和/或时间时,转换失败
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!