[英]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: 这是与以下任何一个相关的错误:
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.