在此代码中,消息框显示数据已成功保存,但在数据库中没有显示数据。
namespace Login_Form
{
public partial class frmCreateCompany : Form
{
SqlConnection con = null;
SqlDataAdapter adp;
DataSet ds = new DataSet();
SqlCommand cmd = null;
DataTable dt = new DataTable();
public frmCreateCompany()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button4_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=.\\SqlExpress; Integrated Security=True; AttachDbFilename=|DataDirectory|\\Garment.mdf; User Instance=true;");
con.Open();
if (textBox2.Text == "")
{
MessageBox.Show("Please enter Company Name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
textBox2.Focus();
return;
}
if (textBox3.Text == "")
{
MessageBox.Show("Please enter Mailing Name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
textBox3.Focus();
return;
}
if (textBox4.Text == "")
{
MessageBox.Show("Please enter License Number", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
textBox4.Focus();
return;
}
if (textBox5.Text == "")
{
MessageBox.Show("Please enter Post Offcie Number", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
textBox5.Focus();
return;
}
if (textBox6.Text == "")
{
MessageBox.Show("Please enter Location", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
textBox6.Focus();
return;
}
if (textBox7.Text == "")
{
MessageBox.Show("Please enter State", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
textBox7.Focus();
return;
}
if (textBox8.Text == "")
{
MessageBox.Show("Please enter Country", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
textBox8.Focus();
return;
}
if (textBox9.Text == "")
{
MessageBox.Show("Please enter Telephone Number", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
textBox9.Focus();
return;
}
if (textBox10.Text == "")
{
MessageBox.Show("Please enter Fax No.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
textBox10.Focus();
return;
}
if (textBox11.Text == "")
{
MessageBox.Show("Please enter Email id", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
textBox11.Focus();
return;
}
if (textBox12.Text == "")
{
MessageBox.Show("Please enter Website Name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
textBox12.Focus();
return;
}
if (textBox13.Text == "")
{
MessageBox.Show("Please enter Currency Name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
textBox13.Focus();
return;
}
if (AccountingStart.Text == "")
{
MessageBox.Show("Please Select Start date ", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
AccountingStart.Focus();
return;
}
if (AccountingFrom.Text == "")
{
MessageBox.Show("Please Select End date ", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
AccountingFrom.Focus();
return;
}
try
{
//con = new SqlConnection(cs);
//con.Open();
string cb = "INSERT INTO COMPANY_MASTER(C_Name,M_Name,L_No,Tax_No,PO_No,Location,State,Country,Telephone,Fax_No,Email_Id,Website,Currency_Name,Company_logo,APT,APF) VALUES(@d1,@d2,@d3,@d4,@d5,@d6,@d7,@d8,@d9,@d10,@d11,@d12,@d13,@d14,@d15,@d16)";
cmd = new SqlCommand(cb);
cmd.Connection = con;
cmd.Parameters.Add(new SqlParameter("@d1", System.Data.SqlDbType.NChar, 10, "C_Name"));
cmd.Parameters.Add(new SqlParameter("@d2", System.Data.SqlDbType.NChar, 10, "M_Name"));
cmd.Parameters.Add(new SqlParameter("@d3", System.Data.SqlDbType.NChar, 10, "L_No"));
cmd.Parameters.Add(new SqlParameter("@d4", System.Data.SqlDbType.NChar, 10, "Tax_No"));
cmd.Parameters.Add(new SqlParameter("@d5", System.Data.SqlDbType.NChar, 10, "PO_No"));
cmd.Parameters.Add(new SqlParameter("@d6", System.Data.SqlDbType.NChar, 10, "Location"));
cmd.Parameters.Add(new SqlParameter("@d7", System.Data.SqlDbType.NChar, 10, "State"));
cmd.Parameters.Add(new SqlParameter("@d8", System.Data.SqlDbType.NChar, 10, "Country"));
cmd.Parameters.Add(new SqlParameter("@d9", System.Data.SqlDbType.NChar, 10, "Telephone"));
cmd.Parameters.Add(new SqlParameter("@d10", System.Data.SqlDbType.NChar, 10, "Fax_No"));
cmd.Parameters.Add(new SqlParameter("@d11", System.Data.SqlDbType.NChar, 10, "email_Id"));
cmd.Parameters.Add(new SqlParameter("@d12", System.Data.SqlDbType.NChar, 10, "Website"));
cmd.Parameters.Add(new SqlParameter("@d13", System.Data.SqlDbType.NChar, 10, "Currency_Name"));
MemoryStream ms = new MemoryStream();
Bitmap bmpImage = new Bitmap(pictureBox1.Image);
bmpImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] data = ms.GetBuffer();
SqlParameter p = new SqlParameter("@d14", SqlDbType.Image);
p.Value = data;
cmd.Parameters.Add(p);
cmd.Parameters.Add(new SqlParameter("@d15", System.Data.SqlDbType.Date, 30, "APT"));
cmd.Parameters.Add(new SqlParameter("@d16", System.Data.SqlDbType.Date, 30, "APF"));
cmd.Parameters["@d1"].Value = textBox2.Text.Trim();
cmd.Parameters["@d2"].Value = textBox3.Text.Trim();
cmd.Parameters["@d3"].Value = textBox4.Text.Trim();
cmd.Parameters["@d4"].Value = textBox5.Text.Trim();
cmd.Parameters["@d5"].Value = textBox6.Text.Trim();
cmd.Parameters["@d6"].Value = textBox7.Text.Trim();
cmd.Parameters["@d7"].Value = textBox8.Text.Trim();
cmd.Parameters["@d8"].Value = textBox9.Text.Trim();
cmd.Parameters["@d9"].Value = textBox10.Text.Trim();
cmd.Parameters["@d10"].Value = textBox11.Text.Trim();
cmd.Parameters["@d11"].Value = textBox12.Text.Trim();
cmd.Parameters["@d12"].Value = textBox13.Text.Trim();
cmd.Parameters["@d13"].Value = textBox14.Text.Trim();
cmd.Parameters["@d14"].Value = p.Value;
cmd.Parameters["@d15"].Value = AccountingStart.Text.Trim();
cmd.Parameters["@d16"].Value = AccountingFrom.Text.Trim();
cmd.ExecuteNonQuery();
//cmd.ExecuteReader();
con.Close();
MessageBox.Show("Successfully saved ", "Company", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void button1_Click(object sender, EventArgs e)
{
var _with1 = openFileDialog1;
_with1.Filter = ("Images |*.png; *.bmp; *.jpg;*.jpeg; *.gif; *.ico");
_with1.FilterIndex = 4;
//Reset the file name
openFileDialog1.FileName = "";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
pictureBox1.Image=Image.FromFile(openFileDialog1.FileName);
}
}
}
}
答案 0 :(得分:0)
要获得受影响的行数,您必须执行以下检查:
int iResult = cmd.ExecuteNonQuery();
con.Close();
if(iResult > 0)
MessageBox.Show("Successfully saved ", "Company", MessageBoxButtons.OK, MessageBoxIcon.Information);
else
MessageBox.Show("Record not saved ", "Company", MessageBoxButtons.OK, MessageBoxIcon.Error);
<强>更新强>:
在代码中替换此行:
SqlParameter p = new SqlParameter("@d14", SqlDbType.Image);
与
SqlParameter p = new SqlParameter("@d14", SqlDbType.Image, data.Length, "Company_logo");
答案 1 :(得分:0)
我更改了您的数据库代码。看起来您正在设置sql日期类型,但将值指定为字符串。尝试下面的代码,我没有测试过,但它应该可以工作。
DateTime? dtStart = null, dtFrom = null;
try
{
dtStart = DateTime.Parse(AccountingStart.Text.Trim());
}
catch { }
if (!dtStart.HasValue)
{
// error check here.
}
try
{
dtFrom = DateTime.Parse(AccountingFrom.Text.Trim());
}
catch { }
if (!dtFrom.HasValue)
{
// error check here.
}
using (SqlConnection sqlConn = new SqlConnection("Data Source=.\\SqlExpress; Integrated Security=True; AttachDbFilename=|DataDirectory|\\Garment.mdf; User Instance=true;"))
{
sqlConn.Open();
SqlTransaction sqlTransaction = null;
string cb = "INSERT INTO COMPANY_MASTER(C_Name,M_Name,L_No,Tax_No,PO_No,Location,State,Country,Telephone,Fax_No,Email_Id,Website,Currency_Name,Company_logo,APT,APF) VALUES(@d1,@d2,@d3,@d4,@d5,@d6,@d7,@d8,@d9,@d10,@d11,@d12,@d13,@d14,@d15,@d16)";
try
{
SqlCommand cmd = sqlConn.CreateCommand();
cmd.CommandText = cb;
sqlTransaction = sqlConn.BeginTransaction();
cmd.Transaction = sqlTransaction;
cmd.CommandType = CommandType.Text;
cmd.Parameters.Add(new SqlParameter("@1", SqlDbType.NChar, 10, ParameterDirection.Input, false, 0, 0, "C_Name", DataRowVersion.Current, textBox2.Text.Trim()));
cmd.Parameters.Add(new SqlParameter("@2", SqlDbType.NChar, 10, ParameterDirection.Input, false, 0, 0, "M_Name", DataRowVersion.Current, textBox3.Text.Trim()));
cmd.Parameters.Add(new SqlParameter("@3", SqlDbType.NChar, 10, ParameterDirection.Input, false, 0, 0, "L_No", DataRowVersion.Current, textBox4.Text.Trim()));
cmd.Parameters.Add(new SqlParameter("@4", SqlDbType.NChar, 10, ParameterDirection.Input, false, 0, 0, "Tax_No", DataRowVersion.Current, textBox5.Text.Trim()));
cmd.Parameters.Add(new SqlParameter("@5", SqlDbType.NChar, 10, ParameterDirection.Input, false, 0, 0, "PO_No", DataRowVersion.Current, textBox6.Text.Trim()));
cmd.Parameters.Add(new SqlParameter("@6", SqlDbType.NChar, 10, ParameterDirection.Input, false, 0, 0, "Location", DataRowVersion.Current, textBox7.Text.Trim()));
cmd.Parameters.Add(new SqlParameter("@7", SqlDbType.NChar, 10, ParameterDirection.Input, false, 0, 0, "State", DataRowVersion.Current, textBox8.Text.Trim()));
cmd.Parameters.Add(new SqlParameter("@8", SqlDbType.NChar, 10, ParameterDirection.Input, false, 0, 0, "Country", DataRowVersion.Current, textBox9.Text.Trim()));
cmd.Parameters.Add(new SqlParameter("@9", SqlDbType.NChar, 10, ParameterDirection.Input, false, 0, 0, "Telephone", DataRowVersion.Current, textBox10.Text.Trim()));
cmd.Parameters.Add(new SqlParameter("@10", SqlDbType.NChar, 10, ParameterDirection.Input, false, 0, 0, "Fax_No", DataRowVersion.Current, textBox11.Text.Trim()));
cmd.Parameters.Add(new SqlParameter("@11", SqlDbType.NChar, 10, ParameterDirection.Input, false, 0, 0, "email_Id", DataRowVersion.Current, textBox12.Text.Trim()));
cmd.Parameters.Add(new SqlParameter("@12", SqlDbType.NChar, 10, ParameterDirection.Input, false, 0, 0, "Website", DataRowVersion.Current, textBox13.Text.Trim()));
cmd.Parameters.Add(new SqlParameter("@13", SqlDbType.NChar, 10, ParameterDirection.Input, false, 0, 0, "Currency_Name", DataRowVersion.Current, textBox14.Text.Trim()));
using (MemoryStream ms = new MemoryStream())
{
using (Bitmap bmpImage = new Bitmap(pictureBox1.Image))
{
bmpImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] data = ms.GetBuffer();
cmd.Parameters.Add(new SqlParameter("@14", SqlDbType.Image, 10, ParameterDirection.Input, false, 0, 0, "Currency_Name", DataRowVersion.Current, data));
}
}
cmd.Parameters.Add(new SqlParameter("@15", SqlDbType.DateTime, 8, ParameterDirection.Input, false, 0, 0, "APT", DataRowVersion.Current, dtStart));
cmd.Parameters.Add(new SqlParameter("@16", SqlDbType.DateTime, 8, ParameterDirection.Input, false, 0, 0, "APF", DataRowVersion.Current, dtFrom));
cmd.ExecuteScalar();
sqlTransaction.Commit();
MessageBox.Show("Successfully saved ", "Company", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
try
{
sqlTransaction.Rollback();
}
catch { }
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}