2012-10-17 202 views
0

我创建了一个数据库2个表中检索从SQL场,每个表都有一个CompanyID字段,其与海誓山盟通过登录

在我的注册页面公司拥有注册下列信息关系:

  1. CompanyID
  2. 公司名称
  3. 企业类型
  4. 联系人
  5. 电话
  6. 地址
  7. 电子邮件
  8. 密码

公司然后使用登录其公司名称和密码,登录后在一家公司可以发布工作,这个问题我已经在我的jobtable还有一个叫CompanyID

如何从存储在我的公司表中的已注册公司的检索CompanyID,并用它

 SqlParameter p6 = new SqlParameter("CompanyID", 'here'); 

我全码:

protected void Button1_Click(object sender, EventArgs e) 
{ 
    string strcon = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\VC_temps.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"; 
    SqlConnection con = new SqlConnection(strcon); 

    SqlCommand com = new SqlCommand("Store-Jobs", con); 
    com.CommandType = CommandType.StoredProcedure; 
    SqlParameter p1 = new SqlParameter("Job", TextBox1.Text); 
    SqlParameter p2 = new SqlParameter("JobType", DropDownList1.Text); 
    SqlParameter p3 = new SqlParameter("StartDate", TextBox3.Text); 
    SqlParameter p4 = new SqlParameter("Time", TextBox2.Text); 
    SqlParameter p5 = new SqlParameter("JobID", TextBox1.Text.Substring(3).ToUpper()); 
    SqlParameter p6 = new SqlParameter("CompanyID",); 
    SqlParameter p7 = new SqlParameter("PositionFilled", "NO"); 
    SqlParameter p8 = new SqlParameter("Description", TextBox4.Text); 
    com.Parameters.Add(p1); 
    com.Parameters.Add(p2); 
    com.Parameters.Add(p3); 
    com.Parameters.Add(p4); 
    com.Parameters.Add(p5); 
    com.Parameters.Add(p6); 
    com.Parameters.Add(p7); 
    com.Parameters.Add(p8); 
    con.Open(); 
    com.ExecuteNonQuery(); 
    Labelinfo.Text = "Post successful."; 
} 
+0

某处您必须拥有登录公司的会话信息。您可以使用公司名称查询公司表以查找公司ID。 – sgud

回答

2

有几种方法可以做到这一点。由于用户正在登录到某个特定的公司,因此我假定在整个会话期间CompanyId会相同。因此,我建议在登录过程中检索CompanyId并将其存储到Session变量中。然后,您可以随时从该会话变量中获取CompanyId。

另一种方法是将公司名称传入存储过程,并让存储过程使用公司名称值获取CompanyId。

关于次要说明....我喜欢在看到可能有问题的代码时显示最佳做法。如果一切成功,您的代码将正常工作,但如果发生异常,则将保持SQL连接处于打开状态。我曾冒昧重构你的代码,以显示替代...

string strcon = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\VC_temps.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"; 

// Use "using" statement to guarantee connection is closed even if exception occurs 
using (SqlConnection con = new SqlConnection(strcon)) 
{ 
    con.Open(); 
    using (SqlCommand com = con.CreateCommand("Store-Jobs")) 
    { 
     com.CommandType = CommandType.StoredProcedure; 
     com.Parameters.AddWithValue("Job", TextBox1.Text); 
     com.Parameters.AddWithValue("JobType", DropDownList1.Text); 
     com.Parameters.AddWithValue("StartDate", TextBox3.Text); 
     com.Parameters.AddWithValue("Time", TextBox2.Text); 
     com.Parameters.AddWithValue("JobID", TextBox1.Text.Substring(3).ToUpper()); 
     com.Parameters.AddWithValue("CompanyID",); 
     com.Parameters.AddWithValue("PositionFilled", "NO"); 
     com.Parameters.AddWithValue("Description", TextBox4.Text); 
     com.ExecuteNonQuery(); 
    } 
} 
Labelinfo.Text = "Post successful."; 
0

你有两个选择:

  1. 您可以使用公司名称检索从公司ID公司表,使用不同的存储过程,然后像在代码中一样调用Store-Job过程。

  2. 您可以修改Store-Job以获取公司名称而不是公司名称,并在该过程中按公司名称查找公司名称,然后将其用于在工作表上执行插入。

  3. 由于您在登录时进行了某种形式的验证,因此可能是检索公司ID并将其存储在会话中的正确时间(正如Gene S上面所述)。然后您可以检索它并在存储过程中使用它。

既然你可能需要用它在你的应用程序很多地方,以保证被保存或检索数据属于正确的公司,选择3听起来就像是最好的。