如何在C#中的任何字符串[]中添加ms-access数据库列 [英] How to add ms-access database column in any string [] in C#

查看:65
本文介绍了如何在C#中的任何字符串[]中添加ms-access数据库列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 string [] 中添加我的数据库列,但未能这样做。我创建了名为Items的 string [] ,并创建了一个名为autocompletemenu1的控件。现在,当我输入autocompetemenu1.Items = Database Column时,数据库列的内容应填入ITEM string []。我已经尝试但它没有工作所以我只是提到它的一些名称,它正在工作,但它没有采取数据库专栏。我不知道为什么。



这是我用来收集藏品的方法。写的方法是autocompleteMenu1.Items = Ms Access Database专栏



我尝试过的方法:



 [DefaultValue( null )] 
public string []项目
{
获取
{
if (sourceItems == null
返回 null ;
var list = new List< string>();
foreach (AutocmpleteItem item in sourceItems)
list.Add(item.ToString ());
return list.ToArray();
}
set {SetAutocompleteItems( value ); }

}
void AutocompleteText2()
{
autocompleteMenu1.AutcompleteMOD = AutoCompleteMode.Suggest;
autocompleteMenu1.AutocompleteSRC = AutoCompleteSource.CustomSource;
AutoCompleteStringCollection coll2 = new AutoCompleteStringCollection();
.autocompleteMenu1.Font = new 字体( MB SINDHI WEB 11 );
string connString = ConfigurationManager.ConnectionStrings [ dbx的]的ConnectionString。

使用(OleDbConnection con = new OleDbConnection(connString))
{
使用(OleDbCommand cmd = new OleDbCommand( SELECT * from userdic,con))
{
con.Open();
OleDbDataReader dr = cmd.ExecuteReader(); // 错误此行!

while (dr.Read())
{

coll2.Add(dr [ Sindhiwrd2\" ]的ToString()); // Sindhiwrd2将数据加载到coll变量中
}

}

con.Close();
}
autocompleteMenu1.Items = coll2; /// 此处生成的错误无法将String []转换为
/// AutoCompleteStringCollection ....

}



这是字符串[] ,我想用名为Sindhiwrd2的数据库列连接

  .autocompleteMenu1.Items =  new   string  [] {
Jamal
Javed
Jameel
James
Jamshed
abc
< span class =code-string> abcd
Ahmed};

解决方案

在您的代码中放置一个try / catch块,这将使您有机会确定问题所在是:

 OldDbConnection conn =  null ; 
OleDbCommand cmd = null ;
OleDbDataReader reader = null ;
string query = SELECT * FROM userdic ;
尝试
{
使用(conn = new OleDbConnection(connectionstring))
{
conn.Open(); // 您不需要在使用区块中调用Close()

使用(cmd = OleDbCommand(query,conn))
{
// 我知道这是默认值,但你永远不会太安全
cmd.CommandType = CommandType.Text;

reader = cmd.ExecuteReader();

while (reader.Read())
{
/ / ......做点什么......
}
}
}
} // 在此处放置一个断点以防万一不会抛出异常。
catch (例外情况)
{
} // < <<把你的断点放在这里



此时,你可以在断点是时检查你的连接字符串,连接对象,命令对象和读者对象命中。



FWIW,想出一个包含所有OleDB ...调用的静态类可能会很有用。我有一个用于Sql和Access,如果我开始需要OleDB,那么写一个就很容易。这样做的好处是我的向外数据库访问始终是相同的,查询总是返回一个DataSet对象,而我真正需要做的就是使用适合我正在访问的数据库类型的类。


I want to add my one of database's column in string[] but fail to do so. I have created string[] with name "Items" and also create a control with name autocompletemenu1. now when I type autocompetemenu1.Items = Database Column then the contents of database column should filled in ITEM string[]. I have tried but it is not working so I just mention some name in it and it is working but it is not taking database column. I don't know why.

This is the method ITEM I am using to take collections. way to write is autocompleteMenu1.Items = Ms Access Database column

What I have tried:

[DefaultValue(null)]
public string [] Items
{
    get
    {
         if (sourceItems == null)
            return null;
        var list = new List<string>();
        foreach (AutocompleteItem item in sourceItems)
            list.Add(item.ToString());
        return list.ToArray();
    }
    set { SetAutocompleteItems(value); }

}
void AutocompleteText2()
{
    autocompleteMenu1.AutcompleteMOD = AutoCompleteMode.Suggest;
    autocompleteMenu1.AutocompleteSRC = AutoCompleteSource.CustomSource;
    AutoCompleteStringCollection coll2 = new AutoCompleteStringCollection();
    this.autocompleteMenu1.Font = new Font("MB SINDHI WEB", 11);
    string connString = ConfigurationManager.ConnectionStrings["dbx"].ConnectionString;

    using (OleDbConnection con = new OleDbConnection(connString))
    {
        using (OleDbCommand cmd = new OleDbCommand("SELECT  * from userdic", con))
        {
            con.Open();
            OleDbDataReader dr = cmd.ExecuteReader();  //error this line!

            while (dr.Read())
            {

              coll2.Add(dr["Sindhiwrd2"].ToString());        // Sindhiwrd2 coloumns data load into the coll variable
            }

        }

        con.Close();
    }
          autocompleteMenu1.Items = coll2; ///Here error is generated that can not convert String[] to
                                           /// AutoCompleteStringCollection....

}


Here is the string [] that i want to connect with database coloumn named "Sindhiwrd2"

this.autocompleteMenu1.Items = new string[] {
  "Jamal",
  "Javed",
  "Jameel",
  "James",
  "Jamshed",
  "abc",
  "abcd",
  "Ahmed"};

解决方案

Put a try/catch block in your code, and that will give you the opportunity to determine where your problem is:

OldDbConnection conn = null;
OleDbCommand cmd = null;
OleDbDataReader reader = null;
string query = "SELECT * FROM userdic";
try
{
    using (conn = new OleDbConnection(connectionstring))
    {
        conn.Open(); // you don't need to call Close() in a using block

        using (cmd = OleDbCommand(query, conn))
        {
            // I know this is the default value, but you can never be too safe
            cmd.CommandType = CommandType.Text;

            reader = cmd.ExecuteReader();

            while (reader.Read())
            {
                // ... do something ...
            }
        }
    }
} // put a breakpoint here in case an exception is NOT thrown.
catch (Exception ex)
{
} // <<< put your breakpoint here


At that point, you can inspect your connection string, the connection object, the command object, and the reader object when the breakpoint is hit.

FWIW, it might be useful to come up with a static class that contains all of your OleDB... calls. I have one each for Sql and Access, and if I ever develop a need for OleDB, it's an easy matter to write one. The benefit of this is that my outward-facing database access is always the same, queries always return a DataSet object, and all I really have to do is use the class that's appropriate for the type of database I'm accessing.


这篇关于如何在C#中的任何字符串[]中添加ms-access数据库列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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