简体   繁体   English

如何在SQL Server数据库中存储源代码行并通过DataSet访问它

[英]How do I store source code lines in SQL Server Database and access it via DataSet

I want to store the following code into the database: 我想将以下代码存储到数据库中:

fun(...);

int main()
{
    fun(3, 7, -11.2, 0.66);
    return 0;
}
fun(...)
{
    va_list ptr;
    int num;
    va_start(ptr, n);
    num = va_arg(ptr, int);
    printf("%d", num);
}

and then get it back in the dataset and display on a page. 然后将其返回到数据集中并显示在页面上。

As of now I have successfully stored the questions with varchar(MAX) datatype but when I try to get it in the dataset i get the following error: 到目前为止,我已经成功地使用varchar(MAX)数据类型存储了问题,但是当我尝试在数据集中获取问题时,出现以下错误:

Failed To Enable Constraints. One Or More Rows Contain Values Violating Non-null, Unique, Or Foreign-key Constraints.

I am doing this in a ASP.NET web application. 我正在ASP.NET Web应用程序中执行此操作。

EDIT: Here is the Table definition of the table I am inserting the data into 编辑:这是我要将数据插入到表的表定义

数据库定义

The query I am using to insert the data into the table: 我用来将数据插入表中的查询:

con.ConnectionString = constr;
    cmd.Connection = con;
    cmd.CommandText = "insert into QuesTable values(@D1,@D2,@D3,@D4,@D5,@D6,@D7, NULL)";
    cmd.Parameters.Add("@D1", SqlDbType.Int).Value = txtQID.Text;
    cmd.Parameters.Add("@D2", SqlDbType.VarChar).Value = txtques.Text;
    cmd.Parameters.Add("@D3", SqlDbType.VarChar).Value = txtansa.Text;
    cmd.Parameters.Add("@D4", SqlDbType.VarChar).Value = txtansb.Text;
    cmd.Parameters.Add("@D5", SqlDbType.VarChar).Value = txtansc.Text;
    cmd.Parameters.Add("@D6", SqlDbType.VarChar).Value = txtansd.Text;
    cmd.Parameters.Add("@D7", SqlDbType.VarChar).Value = txtcorr.Text;

    con.Open();
    int i = cmd.ExecuteNonQuery();
    con.Close();

And finally the code by which I am extracting the data from the dataset 最后是我从数据集中提取数据的代码

DataSet1.QuesTableDataTable dt = new DataSet1.QuesTableDataTable();
    DataSet1TableAdapters.QuesTableTableAdapter adp = new DataSet1TableAdapters.QuesTableTableAdapter();
    dt = adp.GetData();
    DataTable dtUser = dt.Clone();

Hope the information is helpful. 希望这些信息对您有所帮助。

Since I can't see if you've got any other further constraints on the table, it looks like the value you're inserting into the primary key field (Qid) already exists in the table. 由于我看不到表上是否还有其他约束,因此您要插入主键字段(Qid)的值似乎已经存在于表中。

If you need to create a new row for every entry regardless, it would probably be easier to change the column Qid to maintain its own Identity. 如果无论如何都需要为每个条目创建一个新行,则更改Qid列以维护其自己的标识可能会更容易。 If you need to update an existing value, you'll need to add a separate piece of logic to determine if the primary key value already exists and update or insert accordingly. 如果需要更新现有值,则需要添加单独的逻辑以确定主键值是否已经存在,并进行相应的更新或插入。

That's an error related to either: 这是与以下任何一个相关的错误:

  • A not null field is having a null passed in when you load the data. 加载数据时,非null字段传递了null。
  • A foreign key or unique index check is invalid (for instance, FK value of 9 doesn't exist). 外键或唯一索引检查无效(例如,FK值9不存在)。

If you fill the dataset, the error that is generated should be retrievable from the dataset. 如果填充数据集,则应该可以从数据集中检索生成的错误。

HTH. HTH。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将电子邮件存储到SQL Server数据库中? - How do I store emails into my SQL server database? 如何通过传递数据库名称访问WPF中的SQL Server数据库 - How to access SQL Server database in WPF via passing database name 如何在没有物理访问的情况下将SQL Server数据库“安装”到远程服务器? - How do I “Install” a SQL Server Database to a Remote Server Without Physical Access? 更新数据库源后,如何让 Visual Studio 2010 刷新我的数据集? - How do I get Visual Studio 2010 to refresh my DataSet after I have updated the Database source? 如何创建一个简单的Windows窗体来访问SQL Server数据库? - How do I create a simple Windows form to access a SQL Server database? 如何将ObservableCollection中的数据存储到SQL数据库中? - How do I store data from an ObservableCollection into an SQL database? 如何在SQL Server中存储Point数据类型? - How do I store Point datatype in SQL Server? 如何通过Jet访问Access工作组中受用户名/密码保护的Access数据库? - How do I access an Access database protected by a username/password in Access workgroups via Jet? 如何在数据库SQL Server中存储txt信息 - How to Store txt information in database SQL Server 如何在 SQL 服务器数据库中存储列表 - How to store a list in SQL Server database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM