here is my code
这是我的代码
private void button1_Click(object sender, EventArgs e)
{
try
{
string connectionString = @"Data Source=|DataDirectory|\Database_POS.sdf";
using (SqlConnection connection = new SqlConnection(connectionString))
using (SqlCommand command = connection.CreateCommand())
{
command.CommandText= "INSERT INTO Customer (Email) Values (@email);";
command.Parameters.AddWithValue("@email",textBox_Email.Text);
connection.Open();
command.ExecuteNonQuery();
}
}
catch (SqlException ex)
{
Console.WriteLine(ex.Message);
}
}
here is my design
这是我的设计
I am only trying to save 'email' here.
Here is my sql table, problem is I can not even save a single field into table .
这是我的sql表,问题是我甚至不能将一个字段保存到表中。
Database Design
数据库设计
Now error is this
这是现在的错误
2 个解决方案
#1
1
Some things to check:
有些东西检查:
-
Do you event get an error? If yes, what is the error? Be aware that you are writing the error message to the Console window, but your application is a Windows Application. Does your application have a Console window? If no, check the Output window in Visual Studio. Or simpler yet, use
MessageBox.Show
to show the error.你的事件有错误吗?如果是,错误是什么?请注意,您正在将错误消息写入控制台窗口,但是您的应用程序是Windows应用程序。你的应用程序有控制台窗口吗?如果没有,请检查Visual Studio中的输出窗口。或者更简单,使用MessageBox。显示错误。
-
If program runs without any error, check the return value of
command.ExecuteNonQuery()
. It tells you how many records affected. If it is more that zero, the value is really being stored in the database.如果程序运行无误,请检查command.ExecuteNonQuery()的返回值。它告诉你有多少条记录受到影响。如果值大于0,则该值实际上存储在数据库中。
-
Make sure that you are seeing the contents of the database file that you are inserting to in your program! You may have opened
Database_POS.sdf
from your project directory, but the program inserts the data into anotherDatabase_POS.sdf
in your "bin\Debug" directory.确保您正在查看您在程序中插入的数据库文件的内容!您可能已经打开了Database_POS。来自项目目录的sdf,但是程序将数据插入另一个Database_POS。在“bin\Debug”目录下的sdf。
-- More Help --
——更多的帮助
Change your method to:
改变你的方法:
private void button1_Click(object sender, EventArgs e)
{
try
{
string connectionString = @"Data Source=|DataDirectory|\Database_POS.sdf";
using (SqlConnection connection = new SqlConnection(connectionString))
using (SqlCommand command = connection.CreateCommand())
{
command.CommandText= "INSERT INTO Customer (Email) Values (@email);";
command.Parameters.AddWithValue("@email",textBox_Email.Text);
connection.Open();
var rowsAffected = command.ExecuteNonQuery();
if(rowsAffected > 0)
MessageBox.Show("Data inserted successfully");
else
MessageBox.Show("Nothing inserted into the database!");
}
}
catch (SqlException ex)
{
Console.WriteLine(ex.Message);
MessageBox.Show(ex.ToString()); // for debugging purpose
}
}
#2
1
thanks everyone for answers
感谢每个人的答案
Cannot connect to local database in C#
无法连接到c#中的本地数据库
I got my answer form above link.
我的答案在链接上面。
I am using Compact Database and Trying to connect like SQLServer.
我正在使用紧凑数据库并尝试像SQLServer那样连接。
Now got it working.
现在找到了工作。
here is the answer text ,that solve my question
这是答案,可以解决我的问题
When using a .sdf file (SQL Server Compact Edition), you need to use SqlCeConnection and not SqlConnection (that works against a full SQL Server):
#1
1
Some things to check:
有些东西检查:
-
Do you event get an error? If yes, what is the error? Be aware that you are writing the error message to the Console window, but your application is a Windows Application. Does your application have a Console window? If no, check the Output window in Visual Studio. Or simpler yet, use
MessageBox.Show
to show the error.你的事件有错误吗?如果是,错误是什么?请注意,您正在将错误消息写入控制台窗口,但是您的应用程序是Windows应用程序。你的应用程序有控制台窗口吗?如果没有,请检查Visual Studio中的输出窗口。或者更简单,使用MessageBox。显示错误。
-
If program runs without any error, check the return value of
command.ExecuteNonQuery()
. It tells you how many records affected. If it is more that zero, the value is really being stored in the database.如果程序运行无误,请检查command.ExecuteNonQuery()的返回值。它告诉你有多少条记录受到影响。如果值大于0,则该值实际上存储在数据库中。
-
Make sure that you are seeing the contents of the database file that you are inserting to in your program! You may have opened
Database_POS.sdf
from your project directory, but the program inserts the data into anotherDatabase_POS.sdf
in your "bin\Debug" directory.确保您正在查看您在程序中插入的数据库文件的内容!您可能已经打开了Database_POS。来自项目目录的sdf,但是程序将数据插入另一个Database_POS。在“bin\Debug”目录下的sdf。
-- More Help --
——更多的帮助
Change your method to:
改变你的方法:
private void button1_Click(object sender, EventArgs e)
{
try
{
string connectionString = @"Data Source=|DataDirectory|\Database_POS.sdf";
using (SqlConnection connection = new SqlConnection(connectionString))
using (SqlCommand command = connection.CreateCommand())
{
command.CommandText= "INSERT INTO Customer (Email) Values (@email);";
command.Parameters.AddWithValue("@email",textBox_Email.Text);
connection.Open();
var rowsAffected = command.ExecuteNonQuery();
if(rowsAffected > 0)
MessageBox.Show("Data inserted successfully");
else
MessageBox.Show("Nothing inserted into the database!");
}
}
catch (SqlException ex)
{
Console.WriteLine(ex.Message);
MessageBox.Show(ex.ToString()); // for debugging purpose
}
}
#2
1
thanks everyone for answers
感谢每个人的答案
Cannot connect to local database in C#
无法连接到c#中的本地数据库
I got my answer form above link.
我的答案在链接上面。
I am using Compact Database and Trying to connect like SQLServer.
我正在使用紧凑数据库并尝试像SQLServer那样连接。
Now got it working.
现在找到了工作。
here is the answer text ,that solve my question
这是答案,可以解决我的问题
When using a .sdf file (SQL Server Compact Edition), you need to use SqlCeConnection and not SqlConnection (that works against a full SQL Server):