本文介绍了我如何在表中插入数据在其列中有一个外键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

i编写此代码将文件的文本内容和文件本身存储在数据库中,

但是我有一个存储异物的问题将文本表中的键作为空值



Hi all,
i write this code to store the text content of a file and the file itself in a database ,
but i have a problem that store the foreign key in text table as a null

public string storeText_intoDB(string str )
{
    using (SqlConnection conn = new SqlConnection("Data Source=TOSHIBA-PC\\SQLEXPRESS;" +
    "Initial Catalog=FileDB;Integrated Security=True;Pooling=False"))
    {
        try
        {
            conn.Open();
            //SELECT MAX(column_name) FROM table_name
            string strsql = " select MAX( FileID )from File_Table  ";
            SqlCommand cmd1 = new SqlCommand(strsql, conn);
            object result = cmd1.ExecuteScalar();

            if (result == null)
            {

                return "Error IN FileID";
            }
            else
            {
                SqlDataReader myreader;
                myreader = cmd1.ExecuteReader();

                while (myreader.Read())
                {
                    SqlCommand cmd = new SqlCommand();
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "StoredProcedure1";
                    cmd.Connection = conn;

                    cmd.Parameters.AddWithValue("@FileID", myreader["FileID"]);

                    cmd.Parameters.AddWithValue("@ttext", str);
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("Texts STORED successfully");
                    return "text stored Successfully!!!";
                }
                MessageBox.Show("Texts don't STORED successfully");
                return "My reader not working in text stored !";


            }
        }
        catch (Exception ex)
        {
            return ex.Message;
        }
            finally
            {
                conn.Close();

                conn.Dispose();

            }
        }



        }



但仍然将fileID存储为空值; \ ???

任何人都可以帮我吗???


but still store the fileID as nulls ;\ ???
can anyone help me please???

推荐答案


这篇关于我如何在表中插入数据在其列中有一个外键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 10:07