0
关闭我使用Visual Studio 2008中,微软Server精简3.5和脚本的Windows Mobile 6.5.3专业写的。到数据库的连接之前,数据输入
我有一个连接到一个数据库文件,将数据输入到表单中,当您选择按钮,数据应更新或输入数据写入数据库后,小型脚本。
某处在我的剧本我关把数据到数据库之前的连接。
连接的数据库连接说成功!当我运行脚本生成器时,我不会收到任何错误。关于Windows Mobile模拟器的消息是:“需要的ExecuteNonQuery开放和可用的连接连接的当前状态是关闭的。”
正如你所看到的,我在Visual Studio中的新手。
这里是表脚本:
using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlServerCe;
namespace testdbconnection
{
public partial class Form1 : Form
{
public SqlCeConnection cn = new SqlCeConnection(@"Data Source = C:\Users\...\test.sdf");
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
try
{
cn.Open();
}
catch (SqlCeException ex)
{
MessageBox.Show(ex.Message);
Application.Exit();
}
}
private void TextBoxes_TextChanged(object sender, EventArgs e)
{
if (textBox1.Text != "" && textBox2.Text != "")
{
button1.Enabled = true;
}
else
{
button1.Enabled = false;
}
}
private void button1_Click(object sender, EventArgs e)
{
SqlCeCommand cm = new SqlCeCommand("INSERT INTO tblUsers(userName, age) VALUES (@userName, @age)", cn);
cm.Parameters.AddWithValue("@userName", textBox1.Text);
cm.Parameters.AddWithValue("@age", textBox2.Text);
try
{
int affectedRows = cm.ExecuteNonQuery();
if (affectedRows > 0)
{
MessageBox.Show("Insert success!");
textBox1.Text = "";
textBox2.Text = "";
}
else
{
MessageBox.Show("Insert failed!");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
是'在Form1.design.cs Form1_Load'绑定?在'cn.Open();'行设置检查点。 –