求助,又是“从索引 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;
using System.IO;
namespace 学生学籍管理系统
{
public partial class ManageScore : Form
{
public ManageScore()
{
InitializeComponent();
}
string strcon = "学生学籍管理系统.Properties.Settings.Default.FATEConnectionString";
string FileNamePath = "";
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "" || textBox4.Text == "" || textBox5.Text == "" || textBox6.Text == "")
{
MessageBox.Show("请输入完整!");
return;
}
string sqlStr;
SqlConnection conn = new SqlConnection(strcon);
if (FileNamePath != "") //如果选择了照片
{
sqlStr = "insert [chengjiguanli]([xh],[xm],[jsm],[kcmc],[kclx],[fs],[zt],[zp])values(@xh,@xm,@jsm,@kcmc,@kclx,@fs,@zt,@zp)"; //设置Sql语句
}
else //如果没现在照片
{
sqlStr = "insert [chengjiguanli]([xh],[xm],[jsm],[kcmc],[kclx],[zt],[zp])values(@xh,@xm,@jsm,@kcmc,@kclx,@zt,)"; //设置Sql语句
}
SqlCommand cmd = new SqlCommand(sqlStr, conn);
//添加参数
cmd.Parameters.Add("@xh", SqlDbType.Char, 10).Value = textBox1.Text.Trim();
cmd.Parameters.Add("@xm", SqlDbType.Char, 10).Value = textBox2.Text.Trim();
cmd.Parameters.Add("@kclx", SqlDbType.Char, 10).Value = comboBox1.Text;
cmd.Parameters.Add("@jsm", SqlDbType.Char, 10).Value = textBox3.Text.Trim();
cmd.Parameters.Add("@kcmc", SqlDbType.Char, 10).Value = textBox4.Text.Trim();
cmd.Parameters.Add("@fs", SqlDbType.Int).Value = textBox5.Text.Trim();
cmd.Parameters.Add("@zt", SqlDbType.Char, 10).Value = textBox6.Text.Trim();
if (FileNamePath != "") //如果选择了照片
{
FileStream fs = null; //以文件流方式读取照片
fs = new FileStream(FileNamePath, FileMode.Open, FileAccess.Read);
MemoryStream mem = new MemoryStream(); //实例化内存流对象mem
byte[] data1 = new byte[fs.Length]; //定义照片长度的数组
fs.Read(data1, 0, (int)fs.Length); //把照片存到数组中
cmd.Parameters.Add("@zp", SqlDbType.Image); //这里选择Image类型
cmd.Parameters["@zp"].Value = data1;
}
try
{
conn.Open(); //打开数据库连接
cmd.ExecuteNonQuery(); //执行SQL语句
MessageBox.Show("保存成功!");
//重新绑定dataGridView1,此段代码在配置数据源时机器自动生成,在Form1_Load方法中
this.chengjiguanliTableAdapter.Fill(this.fATEDataSet.chengjiguanli);
}