代码如下:
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);
}
catch (Exception ex)
{ MessageBox.Show("出错!" + ex.Message); }
finally
{
conn.Close(); //关闭数据库连接
FileNamePath = "";
}
} private void ManageScore_Load(object sender, EventArgs e)
{
// TODO: 这行代码将数据加载到表“fATEDataSet.chengjiguanli”中。您可以根据需要移动或移除它。
this.chengjiguanliTableAdapter.Fill(this.fATEDataSet.chengjiguanli); }
}
}红色为提示部分,代码是我从书上直接抄的,没错,但数据无法添加,并显示标题上说的错误
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);
}
catch (Exception ex)
{ MessageBox.Show("出错!" + ex.Message); }
finally
{
conn.Close(); //关闭数据库连接
FileNamePath = "";
}
} private void ManageScore_Load(object sender, EventArgs e)
{
// TODO: 这行代码将数据加载到表“fATEDataSet.chengjiguanli”中。您可以根据需要移动或移除它。
this.chengjiguanliTableAdapter.Fill(this.fATEDataSet.chengjiguanli); }
}
}红色为提示部分,代码是我从书上直接抄的,没错,但数据无法添加,并显示标题上说的错误
解决方案 »
- 关于用ado.net批量插入数据到oracle的效率问题
- 请教关于DataGrid的问题
- 急呀,急呀,如何生成以下格式的XML文件呀
- 树和datagrid的问题
- 索引超出范围。必须为非负值并小于集合大小??撒意识阿 ?在线等待在线等待在线等待在线等待在线等待
- 导出到excel问题-怎样指定导出的数据类型??
- 十万火急!没装office xp时,vs.net运行正常;装了office xp后,vs.net就自动退出了。
- dataset导excel的速度是多少?
- 请教如果和在<%@ 指令中处理预定义字符串
- ◎◎请教高手!repeater控件没有DataKeys属性,我如何能获取某行的编号?
- 有没有办法让vs2008中的calendar控件只能选择月份而不让他选择日?
- url传值的问题
string strcon = 学生学籍管理系统.Properties.Settings.Default.FATEConnectionString;
看看你的连接字符串有没有问题