简体   繁体   English

C#填充数据表

[英]c# fill dataTable

I have a textbox window1.xaml that works as a searchbox. 我有一个文本框window1.xaml用作搜索框。 I am using a integrated sql database in my project and I have a dataset.xsd as well with tabel adapters and queries. 我在项目中使用集成的sql数据库,并且具有数据集.xsd以及表格适配器和查询。

Whenever I need to query the database I do something like this: 每当我需要查询数据库时,我都会执行以下操作:

BooksTableAdapter tableAdapterBooks = new BooksTableAdapter();
dataSetLibrary.BooksDataTable dataTableBooks;
dataTableBooks = tableAdapterBooks.getDataByTitle(searchText);

For this searchbox I have the following code. 对于此搜索框,我具有以下代码。

string[] allWords = txtSearch.Text.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
string sql = "SELECT Books.ISBN, Books.Title, Books.Tag, Books.Image, Books.photoType, Publishers.Name AS publisherName FROM Books INNER JOIN Publishers ON Books.codPublisher = Publishers.codPublisher WHERE ";
using (SqlCommand command = new SqlCommand())
{
    for (int i = 0; i < allWords.Length; ++i)
    {
          if (i > 0)
          sql += "OR ";

          string paramName = "@param" + i.ToString();
          sql += string.Format("(Books.Title LIKE {0}) ", paramName);
          command.Parameters.AddWithValue(paramName, allWords[i] + "%");
     }
     command.CommandText = sql;
 }

How can I use the command to query my database and fill dataTableBooks? 如何使用命令查询数据库并填充dataTableBooks?

After hours around this, I have come up with this solution. 经过几个小时,我提出了这个解决方案。

private SqlConnection sqlConn = new SqlConnection();
private System.Data.DataSet dataSet = new System.Data.DataSet();
private System.Data.DataTable dataTable;
private System.Data.DataRow dataRow;

private SqlCommand search(string searchParam, int searchOption)
    {
        SqlCommand command = new SqlCommand();
        string sql;
        string[] allWords = searchParam.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
        if (searchOption == 1)
        {
            sql = "SELECT Livros.ISBN, Livros.Titulo, Livros.Tema, Livros.Resumo, Livros.Imagem, Livros.fotoTipo, Editoras.Nome AS nomeEditora FROM Livros INNER JOIN Editoras ON Livros.codEditora = Editoras.codEditora WHERE ";
        }
        else
        {
            sql = "SELECT Livros.ISBN, Livros.Titulo, Livros.Tema, Livros.Resumo, Livros.Imagem, Livros.fotoTipo, Editoras.Nome AS nomeEditora FROM Livros INNER JOIN livrosAutores ON Livros.ISBN = livrosAutores.ISBN INNER JOIN Autores ON livrosAutores.idAutor = Autores.idAutor INNER JOIN Editoras ON Livros.codEditora = Editoras.codEditora WHERE ";
        }
        using (command)
        {
            for (int i = 0; i < allWords.Length; ++i)
            {
                if (i > 0)
                {
                    sql += "OR ";
                }

                if (searchOption == 1)
                {
                    sql += string.Format("(Livros.Titulo LIKE '%{0}%') ", allWords[i]);
                }
                else
                {
                    sql += string.Format("(Livros.Autor LIKE '%{0}%') ", allWords[i]);
                }
            }
            command.CommandText = sql;
        }
        return command;
    }

protected void Bind()
    {
            sqlConn.ConnectionString = Properties.Settings.Default.BibliotecaConnectionString;
            string connectionString = sqlConn.ConnectionString.ToString();
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(search(searchText, searchOption).CommandText, connectionString);
            sqlDataAdapter.Fill(dataSet, "livrosTitulo");
            dataTable = dataSet.Tables["livrosTitulo"];
            dataGrid.DataContext = dataTable.DefaultView;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM