无法从本地数据库中检索数据? [英] Can't retrieve data from local database?

查看:151
本文介绍了无法从本地数据库中检索数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习C#

而我正在尝试创建简单的词典,将英语单词翻译成我的母语。

我无法理解我在哪里制作错误,我想从本地数据库中检索数据,当用户按下按钮button1

现在它显示这样的消息:你的应用程序中发生了未处理的异常...

以下是我的程序代码:





  private   void  button1_Click( object  sender,EventArgs e)
{

尝试
{

SqlCeConnection con = new SqlCeConnection( c:\\users \\\\\\\\\\\\\\\\\\\\\工作室2012 \\\Projects\\Sozluk\\Sozluk\\dictionary.sdf);
SqlCeCommand com = new SqlCeCommand( 选择来自sozler的soz,terjime,mysal soz =' + soz1.Text + ',con);
con.Open();

SqlCeDataReader reader = com.ExecuteReader();

if (reader.Read())
{
soz1.Text = reader.GetString( 0 );
terjime1.Text = reader.GetString( 1 );
mysal1.Text = reader.GetString( 2 );
}
else
{

MessageBox.Show( 无效的单词 字典,MessageBoxButtons.OK,MessageBoxIcon.Error);
}


}
catch (SqlCeException)
{
MessageBox .Show( 错误); // 这将是一个改进,而不仅仅是'错误'这个词
}


}

解决方案

请参考以下几乎可以解释您所需要的内容的链接。



http:// technet.microsoft.com/en-us/library/aa226134(v=SQL.80).aspx [ ^ ]



我在我的机器上尝试过它有效。



我认为创建的表(sozler)或连接字符串中存在问题。


第一个问题是SQL数据库紧凑吗?你真的确定你是假设使用SqlCeConnection而不是SqlConnection等。顺便说一句,这不会导致错误,但如果它错了它无论如何都不会工作:-)



接下来你需要回过头来阅读字符串基础知识



最后如果你确实需要使用SqlCeConnection,那么至少要查看底部的Microsoft示例关于API调用的帮助



这里提示滚动到底部=> http://msdn.microsoft。 com / en-us / library / system.data.sqlserverce.sqlceconnection.connectionstring%28v = vs.100%29.aspx [ ^ ]


< blockquote>以下链接解释了如何部署SQLServer Compact

http://msdn.microsoft.com/en-us/library/aa983326(v = vs.90).aspx [ ^ ]



这个是解释如何在部署中添加数据库



http://msdn.microsoft.com/en-us/lib rary / aa983340(v = vs.90).aspx [ ^ ]



希望它解决问题


I am learning C#
and I am trying to create simple dictionary which will translate words from english to my native language.
I can't understand where i made mistake, i want to retrieve data from local database when user will press button "button1"
now it shows such message: Unhandled exception has occured in your application...
The following are my code of program:


private void button1_Click(object sender, EventArgs e)
        {
            
                try
                {

                    SqlCeConnection con = new SqlCeConnection("c:\\users\\rustam\\documents\\visual studio 2012\\Projects\\Sozluk\\Sozluk\\dictionary.sdf");
                    SqlCeCommand com = new SqlCeCommand("Select soz, terjime, mysal from sozler where soz='" + soz1.Text + "'", con);
                    con.Open();

                    SqlCeDataReader reader = com.ExecuteReader();

                    if (reader.Read())
                    {
                        soz1.Text = reader.GetString(0);
                        terjime1.Text = reader.GetString(1);
                        mysal1.Text = reader.GetString(2);
                    }
                    else
                    {

                        MessageBox.Show("Invalid Word", "Dictionary", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }


                }
                catch (SqlCeException)
                {
                    MessageBox.Show("Error"); // Would be an improvement than just the word 'Error'
                }


           } 

解决方案

Please refer to the following link that explains almost everything that you need.

http://technet.microsoft.com/en-us/library/aa226134(v=SQL.80).aspx[^]

I tried it in my machine and it worked.

I think there is a problem in the created table(sozler) or maybe in the connection string.


1st question is the SQL database compact? Are you really sure you are suppose to be using SqlCeConnection rather than SqlConnection etc. Thats not causing the error by the way but if it's wrong it wont work anyhow :-)

Next you need to go back and read up again on string basics

Finally if you really do need to use SqlCeConnection then at least look at the Microsoft sample at the bottom of the help on the API call

HINT HERE SCROLL TO BOTTOM => http://msdn.microsoft.com/en-us/library/system.data.sqlserverce.sqlceconnection.connectionstring%28v=vs.100%29.aspx[^]


the following link is explaining how to deploy SQLServer Compact
http://msdn.microsoft.com/en-us/library/aa983326(v=vs.90).aspx[^]

and this one is explaining how to add your database in the deployment

http://msdn.microsoft.com/en-us/library/aa983340(v=vs.90).aspx[^]

Hope it solves the issue


这篇关于无法从本地数据库中检索数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆