求助:从字符串转换日期和/或时间时,转换失败。sql语句没问题呀?

92浩 2015-03-18 07:45:31
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;
}

}
}

...全文
396 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
92浩 2015-03-18
  • 打赏
  • 举报
回复
引用 4 楼 starfd 的回复:
个人建议你最好用DbParameter的方式传递
不知道怎么用
92浩 2015-03-18
  • 打赏
  • 举报
回复
引用 2 楼 starfd 的回复:
dtBirthday.Value取到的是DateTime,你在format里面设置下格式,比如{0:yyyy-MM-dd}
我改了,果然解决了,可这是为什么呢?
  • 打赏
  • 举报
回复
个人建议你最好用DbParameter的方式传递
92浩 2015-03-18
  • 打赏
  • 举报
回复
异常指向的是
 int n = comm.ExecuteNonQuery();//执行“添加”命令,返回值为更新的行数
  • 打赏
  • 举报
回复
dtBirthday.Value取到的是DateTime,你在format里面设置下格式,比如{0:yyyy-MM-dd}
92浩 2015-03-18
  • 打赏
  • 举报
回复
开始我以为是'{3}'是不是不要单引号,改了后又显示“星期二附近有错误”,大神们帮忙看看啊

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧