上载包含某些详细信息的文件时,它们在sql db中放了2行

qhhrdooz  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(216)

用一个保存按钮插入数据和文件上传后,在数据库中创建2行。我想把两排都排成一排。请建议。详情见附件。应用程序gui
设计与数据库

{
  con.Open ();
  SqlCommand cmd = new SqlCommand ("spStockInWard", con);
  cmd.CommandType = CommandType.StoredProcedure;
  //cmd.Parameters.AddWithValue("@Action", "Insert");
  cmd.Parameters.AddWithValue ("@SerialNumber", textBox1.Text.Trim ());
  cmd.Parameters.AddWithValue ("@AssetType", comboBox1.Text);
  cmd.Parameters.AddWithValue ("@AssetMake", comboBox2.Text);
  cmd.Parameters.AddWithValue ("@AssetModel", textBox2.Text);
  cmd.Parameters.AddWithValue ("@HDDSize", comboBox5.Text);
  cmd.Parameters.AddWithValue ("@RAMSize", comboBox6.Text);
  cmd.Parameters.AddWithValue ("@MonitorType", comboBox7.Text);
  cmd.Parameters.AddWithValue ("@StockReceivedDate", textBox3.Text);
  cmd.Parameters.AddWithValue ("@InvoiceDate", textBox4.Text);
  cmd.Parameters.AddWithValue ("@InvoiceNumber", textBox5.Text);
  cmd.Parameters.AddWithValue ("@AssetStatus", comboBox3.Text);
  cmd.Parameters.AddWithValue ("@WorkingStatus", comboBox4.Text);
  cmd.Parameters.AddWithValue ("@Remarks", textBox6.Text);
  cmd.Parameters.Add ("@ERROR", SqlDbType.Char, 500);
  cmd.Parameters["@ERROR"].Direction = ParameterDirection.Output;
  cmd.ExecuteNonQuery ();
  string message = (string) cmd.Parameters["@ERROR"].Value;
  MessageBox.Show (message.ToString (), "Userinfo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  con.Close ();
} else
{
  MessageBox.Show ("Please Fill All Details!");
}
try {
  string filename = System.IO.Path.GetFileName (openFileDialog1.FileName);
  if (filename == null) {
    MessageBox.Show ("Please select a valid document.");
  } else {

    con.Open ();
    SqlCommand cmd = new SqlCommand ("insert into tblStockInWard (document) values('D:\\AMS-Docs\\StockInWard\\" + filename + "')", con);
    string path = Application.StartupPath.Substring (0, (Application.StartupPath.Length - 50));
    System.IO.File.Copy (openFileDialog1.FileName, @"D:\\AMS-Docs\\StockInWard\\" + filename);

    cmd.ExecuteNonQuery ();
    con.Close ();
    MessageBox.Show ("Document uploaded.");
  }
} catch (Exception ex) {
  MessageBox.Show (ex.Message);
}

这些都是我的密码。

sycxhyv7

sycxhyv71#

正如我在评论中所说的,你需要先插入你的详细信息,然后你需要用剩下的信息更新你刚刚插入的那一行(或者更简单一点,为什么你不能设置 document 首字母中的列 INSERT ,也是??)。
另外:我强烈建议(a)不要使用 .AddWithValue() ,并对参数使用适当的数据类型—因为您还没有向我们展示您的存储过程,所以我只能猜测这些存储过程可能是什么—根据需要进行调整。

// get the "serial no" - assuming this is the**primary key**for the table
string serialNo = textBox1.Text.Trim();

// create your INSERT stored procedure call
SqlCommand cmd = new SqlCommand("dbo.spStockInWard", con);
cmd.CommandType = CommandType.StoredProcedure;

// add parameters with the ".Add()" method, also specifying the datatype used    
cmd.Parameters.Add("@SerialNumber", SqlDbType.VarChar, 50).Value = serialNo;
cmd.Parameters.Add("@AssetType", SqlDbType.VarChar, 50).Value = comboBox1.Text;
cmd.Parameters.Add("@AssetMake", SqlDbType.VarChar, 50).Value = comboBox2.Text;
cmd.Parameters.Add("@AssetModel", SqlDbType.VarChar, 50).Value = textBox2.Text;
cmd.Parameters.Add("@HDDSize", SqlDbType.Int).Value = Convert.ToInt32(comboBox5.Text);
cmd.Parameters.Add("@RAMSize", SqlDbType.Int).Value = Convert.ToInt32(comboBox6.Text);
cmd.Parameters.Add("@MonitorType", SqlDbType.VarChar, 50).Value = comboBox7.Text;
cmd.Parameters.Add("@StockReceivedDate", SqlDbType.DateTime).Value = DateTime.Parse(textBox3.Text);
cmd.Parameters.Add("@InvoiceDate", SqlDbType.DateTime).Value = DateTime.Parse(textBox4.Text);
cmd.Parameters.Add("@InvoiceNumber", SqlDbType.VarChar, 50).Value = textBox5.Text;
cmd.Parameters.Add("@AssetStatus", SqlDbType.VarChar, 50).Value = comboBox3.Text;
cmd.Parameters.Add("@WorkingStatus", SqlDbType.VarChar, 50).Value = comboBox4.Text;
cmd.Parameters.Add("@Remarks", SqlDbType.VarChar, 500).Value = textBox6.Text;

cmd.Parameters.Add("@ERROR", SqlDbType.Char, 500);
cmd.Parameters["@ERROR"].Direction = ParameterDirection.Output;

// open connection, insert details
con.Open();
cmd.ExecuteNonQuery();

string message = (string)cmd.Parameters["@ERROR"].Value;

MessageBox.Show(message.ToString(), "Userinfo", MessageBoxButtons.OK, MessageBoxIcon.Warning);

// second step -**UPDATE**the newly inserted row with your file info
string filename = System.IO.Path.GetFileName(openFileDialog1.FileName);

if (filename == null)
{
    MessageBox.Show("Please select a valid document.");
    con.Close();
}
else
{
    // define the UPDATE statement against the same row as inserted above       
    SqlCommand cmd = new SqlCommand("UPDATE tblStockInWard SET document = @document WHERE SerialNumber = @SerialNo";

    // re-use "serial no" from above
    cnd.Parameters.Add("@SerialNo", SqlDbType.VarChar, 50).Value = serialNo;
    cmd.Parameters.Add("@document", SqlDbType.VarChar, 250).Value = "D:\\AMS-Docs\\StockInWard\\" + filename;

    cmd.ExecuteNonQuery();

    con.Close();
    MessageBox.Show("Document uploaded.");
}

相关问题