为什么我不能获取当前ID并放在另一个表中?

时间:2016-11-26 10:46:09

标签: c# asp.net

我正在尝试创建一个简单的新闻和图像系统,我首先需要使用SCOPE_IDENTITY()并执行标量,但我没有太多运气。我得到了:

  

当前上下文中不存在名称“newID”

 protected void btnUpload_Click(object sender, EventArgs e)
    {
        if (FileUpload1.PostedFile != null)
        {
            string FileName = Path.GetFileName(FileUpload1.PostedFile.FileName);  

            //Save files to disk
            FileUpload1.SaveAs(Server.MapPath("/images/admin/news/" +  FileName));

            //Add Entry to DataBase
            String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
            int newID = 0;

            string strQuery = @"insert into tblFiles (FileName, FilePath) values(@FileName, @FilePath); select cast(scope_identity() As int);";

            using (SqlConnection connection = new SqlConnection(strConnString))
            using (SqlCommand command = new SqlCommand(strQuery, connection))
            {
                command.CommandType = CommandType.Text;
                command.Parameters.Add("@FileName", SqlDbType.VarChar).Value = FileName;
                command.Parameters.Add("@FilePath", SqlDbType.VarChar).Value = "/images/admin/news/" + FileName;

                try
                {
                    connection.Open();
                    newID = (int)command.ExecuteScalar();
                }
                catch 
                {
                }
            }
        }

        if (newID > 0)

        {
            string strAddNewsQuery = @"insert into tblNews (newsTitle, newsDate, newsSummary, newsContent, newsPicID) 
                    values(@newsTitle, @newsDate, @newsSummary, @newsContent, @newsPicID)"; 
            using (SqlConnection connection = new SqlConnection(strConnString))
            using (SqlCommand command = new SqlCommand(strAddNewsQuery, connection))
            {
                command.CommandType = CommandType.Text;
                command.Parameters.Add("@newsTitle", SqlDbType.VarChar).Value = FileName;
                command.Parameters.AddWithValue("@newsDate", txtnewsdate.Text);            
                command.Parameters.AddWithValue("@newsSummary", txtnewssummary.Text);            
                command.Parameters.AddWithValue("@newsContent", txtnewsmaincontent.Text);  
                command.Parameters.Add("@newsPicID", SqlDbType.Int).Value = newID;

                try
                {
                    connection.Open();
                    command.ExecuteNonQuery();
                }
                catch
                {
                }
                finally {
                    connection.Close();
                    connection.Dispose();   
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:2)

int没有您可以访问的属性。改变

command.Parameters.AddWithValue("@newsPicID", newID.Value); 

command.Parameters.AddWithValue("@newsPicID", newID); 

更好的方法是使用指定数据库值类型的参数。

command.Parameters.Add("@newsPicID", SqlDbType.Int).Value = newID;

但是您要尝试获取SCOPE_IDENTITY()tblNews,而不是tblFilestblNews作为newsPicID使用SCOPE_IDENTITY()。您需要从第一个数据库命令获取SqlCommand cmd = new SqlCommand(strQuery, con)

<强>更新

您需要为该命令分配连接。

using

更新2

这是一个完整的片段,可以帮助您入门。请注意int newID = 0; using (SqlConnection connection = new SqlConnection(strConnString)) using (SqlCommand command = new SqlCommand(strQuery, connection)) { command.CommandType = CommandType.Text; command.Parameters.Add("@FileName", SqlDbType.VarChar).Value = FileName; command.Parameters.Add("@FilePath", SqlDbType.VarChar).Value = "/images/admin/news/" + FileName; try { connection.Open(); newID = (int)command.ExecuteScalar(); } catch { } } if (newID > 0) { using (SqlConnection connection = new SqlConnection(strConnString)) using (SqlCommand command = new SqlCommand(strAddNewsQuery, connection)) { command.CommandType = CommandType.Text; command.Parameters.Add("@newsTitle", SqlDbType.VarChar).Value = FileName; //etc command.Parameters.Add("@newsPicID", SqlDbType.Int).Value = newID; try { connection.Open(); command.ExecuteNonQuery(); } catch { } } } 的换行。这确保了正确处理连接。

    int numberOfDigits(int n)
    {
    if(n==0)
    return 0;
    else
    return numberOfDigits(n/10)+1;
    }