Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/sharepoint/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用SqlCeDataAdapter在c中向本地数据库插入新行_C#_Database - Fatal编程技术网

C# 使用SqlCeDataAdapter在c中向本地数据库插入新行

C# 使用SqlCeDataAdapter在c中向本地数据库插入新行,c#,database,C#,Database,我有一个本地SQL Server CE数据库mainDB.sdf,只有一个名为Users的表 它由7列组成: 名称ntext 姓ntext 昵称nchar,唯一,主键 性别背景 状态文本 城市文本 照片图像 我使用对象: private SqlCeConnection connection; private SqlCeDataAdapter adapter; private SqlCeCommandBuilder builder; private DataSet data; 首先,我连接到一个

我有一个本地SQL Server CE数据库mainDB.sdf,只有一个名为Users的表

它由7列组成:

名称ntext 姓ntext 昵称nchar,唯一,主键 性别背景 状态文本 城市文本 照片图像 我使用对象:

private SqlCeConnection connection;
private SqlCeDataAdapter adapter;
private SqlCeCommandBuilder builder;
private DataSet data;
首先,我连接到一个数据库:

connection = new SqlCeConnection("Data Source = mainDB.sdf");
connection.Open();
SqlCeCommand cmd = connection.CreateCommand();
cmd.CommandText = "SELECT * FROM Users";
adapter = new SqlCeDataAdapter(cmd);
builder = new SqlCeCommandBuilder();
builder.DataAdapter = adapter;
data = new DataSet();
adapter.Fill(data);
我尝试插入新行:

MemoryStream ms = new MemoryStream();
pictureBox1.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
byte[] im = ms.ToArray();
DataRow newRow = data.Tables[0].NewRow();
newRow["Name"] = textBox1.Text;
newRow["Surname"] = textBox2.Text;
newRow["Nickname"] = textBox3.Text;
newRow["Gender"] = listBox1.Text.Length > 0 ? listBox1.Text : null;
newRow["Status"] = radioButton1.Checked ? "Student" : radioButton2.Checked ? "Professional" : null;
newRow["City"] = comboBox1.Text.Length > 0 ? comboBox1.Text : null;
newRow["Photo"] = im;
data.Tables[0].Rows.Add(newRow);
adapter.Update(data);
connection.Close();
之后,我转到表Users,没有新数据。 我认为SqlCeCommandBuilder应该生成所需的命令。我错过了什么

重要编辑: 一切正常。似乎c创建了两个到数据库的链接。第一个在名为mainDB.sdf的项目文件夹中,它是空的。但bin\Debug文件夹中还有一个。作为一个绝对的新手,我不知道这一点。我可以在DB mainDB.sdf1中获取所有以前的行,选择New Query并输入SELECT*FROM USERS。与第一个mainDB.sdf相同的命令没有给出任何结果。这就是问题所在。

使用SqlCeCommand


我试图用您的代码替换代码:[DataRow…adapter.Updatedata;]的这部分。事实上,我有两个问题。首先,我得到参数@gender的ArgumentNullException,因为该字段允许NULL。其次,如果提供了所有字段,则在运行程序后,我看不到数据库中的任何更改。我可以尝试做什么?您也可以尝试通过以下方式指定参数:com.parameters.AddnewSqlCeParameter@name,SqlDbType.NText,100{Value=textBox1.Text};如果您的连接处于活动状态,并且cmd.ExecuteNonQuery没有引发任何异常,那么您应该在SqlCe数据库中看到新记录!ExecuteOnQuery返回一个整数,该整数是受影响的行数。也检查一下。在更改参数的规格之后,我的程序仍然强制输入所有字段,尽管它不应该这样做。cmd.ExecuteNonQuery返回1,但我看不到结果。我连接mainDB.sdf并在服务器资源管理器中对用户使用显示表数据。只有一行包含所有NULL。您可以检查最后插入数据库的行:var rowId=new SqlCeCommandSELECT@@IDENTITY,connection.ExecuteScalar;为列使用适当的数据类型。nText用于非常大的文本,而不是用于几乎不超过10个字符的名称。
SqlCeConnection connection = new SqlCeConnection("Data Source = mainDB.sdf");
connection.Open();

using (SqlCeCommand com = new SqlCeCommand("INSERT INTO Users (Name, Surname, Nickname, Gender, Status, City, Photo) Values(@name,@surname,@nickname,@gender,@status,@city,@photo)", connection))
{
    com.Parameters.AddWithValue("@name", textBox1.Text);
    com.Parameters.AddWithValue("@surname", textBox2.Text);
    com.Parameters.AddWithValue("@nickname", textBox3.Text);
    com.Parameters.AddWithValue("@gender", listBox1.Text.Length > 0 ? listBox1.Text : null);
    com.Parameters.AddWithValue("@status", radioButton1.Checked ? "Student" : radioButton2.Checked ? "Professional" : null);
    com.Parameters.AddWithValue("@city", comboBox1.Text.Length > 0 ? comboBox1.Text : null);
    com.Parameters.AddWithValue("@photo", im);

    com.ExecuteNonQuery();
}

connection.Close();