我在C#中编写程序以将信息插入Access数据库。因此,使用以下C#语句和处理表单,我需要知道我的Access字段必须是什么数据类型。 (即; {0} = int,{1} = nchar) 我想,直到我明白了,
![string vsql = string.Format("insert into Log values " +
"('{0}','{1}','{2}','{3}','{4}','{5}','{6}')",
comboBox1.Text, //(type?)
comboBox2.Text, //(type?)
int.Parse(textBox1.Text), //I want this to be float (possible?)
int.Parse(textBox1.Text), //I want this to be float (possible?)
textBox2.Text, //(type?)
textBox3.Text, //(type?)
addRemove); //(type?)
答案 0 :(得分:1)
string vsql = string.Format("insert into Log values " +
"('{0}','{1}',{2},{3},'{4}',#{5}#,'{6}')",
comboBox1.Text, //string field
comboBox2.Text, //string field
int.Parse(textBox1.Text), //don't wrap this in quotes if you want it as a float
int.Parse(textBox1.Text), //same as above
textBox2.Text, //this is a DateTime field - you probably need to wrap with # marks.
textBox3.Text, //string field
addRemove //bool field
);
答案 1 :(得分:0)
private void SaveRecord()
{
string connString = ConfigurationManager.ConnectionStrings["dbx"].ConnectionString;
string cmdString = "INSERT INTO tblBasicInfo (SName, FName, Gender, DOB, DOBnW, Nationality, FCNICNo, FOccup, Religion, Cell, Fone, Address, AdmNo, ClassofAdm, ClassofReading, TranFacility, Remarks, TransportChr, Picture, SpecialRemarks) Values (@SName, @FName, @Gender, @DOB, @DOBnW, @Nationality, @FCNICNo, @FOccup, @Religion, @Cell, @Fone, @Address, @AdmNo, @ClassofAdm, @ClassofReading, @TranFacility, @Remarks, @TransportChr, @Picture, @SpecialRemarks)";
using (OleDbConnection con = new OleDbConnection(connString))
{
using (OleDbCommand cmd = new OleDbCommand(cmdString, con))
{
con.Open();
cmd.Parameters.AddWithValue("@SName", NametextBox.Text);
cmd.Parameters.AddWithValue("@FName", FNametextBox.Text);
cmd.Parameters.AddWithValue("@Gender", GendercomboBox.Text);
cmd.Parameters.AddWithValue("@DOB", (InFiguredateTimePicker.Text));
cmd.Parameters.AddWithValue("@DOBnW", InWordstextBox.Text);
cmd.Parameters.AddWithValue("@Nationality", NationalitytextBox.Text);
cmd.Parameters.AddWithValue("@FCNICNo", FCNICNotextBox.Text);
cmd.Parameters.AddWithValue("@FOccup", FOccuptextBox.Text);
cmd.Parameters.AddWithValue("@Religion", ReligiontextBox.Text);
cmd.Parameters.AddWithValue("@Cell", CelltextBox.Text);
cmd.Parameters.AddWithValue("@Fone", PhonetextBox.Text);
cmd.Parameters.AddWithValue("@Address", AddresstextBox.Text);
cmd.Parameters.AddWithValue("@AdmNo", AdmNotextBox.Text);
cmd.Parameters.AddWithValue("@ClassofAdm", ClassofAdmcomboBox.Text);
cmd.Parameters.AddWithValue("@DateofAdm", (DateofAdmdateTimePicker.Text));
cmd.Parameters.AddWithValue("@ClassofReading", ClassOfReadcomboBox.Text);
cmd.Parameters.AddWithValue("@TranFacility", TransportFacilitytextBox.Text);
cmd.Parameters.AddWithValue("@Remarks", RemarkscomboBox.Text);
cmd.Parameters.AddWithValue("@TransportChr", TransportFeetextBox.Text);
cmd.Parameters.AddWithValue("@Picture", SavePhoto());
cmd.Parameters.AddWithValue("@SpecialRemarks", SRemarkstextBox.Text);
cmd.ExecuteNonQuery();
MessageBox.Show("Record is Saved Successfully.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
NametextBox.Clear();
FNametextBox.Clear();
NametextBox.Focus();
}
}
}