我如何在表中插入数据在其列中有一个外键? [英] how i can insert data in table has a foreign key in its column ?

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

问题描述

大家好,

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???

推荐答案

你好伙计你呢知道Sql语法'首先尝试使用INSERT INTO语法插入数据,如果它有效则数据库中没有问题,如果不检查你的Index.Index扮演主要角色在表中插入数据。
hello dude do you know Sql Syntax''s first try yo insert the data using INSERT INTO syntax if it works there is no problem in your database if not check your Index.Index plays main role to insert data in your table.


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆