下面提供一段示例程序,供您参考:
private void btnAdd_Click(object sender, System.EventArgs e)
{
// Fill a DataSet
DataSet ds = new DataSet();
string connString = "Server=SHA-RICKIE-01;DataBase=test;uid=user;pwd=user";
string sqlString = "Select * from Students";
SqlConnection conn = new SqlConnection(connString);
conn.Open();
SqlDataAdapter sqlDataAdapter1 = new SqlDataAdapter(sqlString,conn);
SqlCommandBuilder MyCB = new SqlCommandBuilder(sqlDataAdapter1);
sqlDataAdapter1.Fill(ds, "students");
DataTable TheTable = ds.Tables[0];
// Create a new row in Memory
DataRow aRow = TheTable.NewRow();
// Insert the information from the dialog into the Table
aRow["name"] = txtboxname.Text;
aRow["age"] = txtboxage.Text;
// 创建一个FileStream对象用来读取image文件,其中FileName 为该image的完整文件名
string FileName = txtboxFilename.Text;
FileStream fs = new FileStream (FileName, FileMode.OpenOrCreate,
FileAccess.Read);
// Read the Data into the Byte Array
byte[] MyData = new byte[fs.Length];
fs.Read(MyData, 0, (int)fs.Length);
fs.Close();
// Assign the DataRow Picture Column to the Byte Array to populate it in the DataRow
aRow["image"] = MyData;
// Add the DataRow to the DataTable
TheTable.Rows.Add(aRow);
// 写数据记录到数据库表中
sqlDataAdapter1.Update(ds, "students");
MessageBox.Show("A student added.");
}