c#fill dataTable

时间:2015-04-14 17:37:29

标签: c# sql wpf dataset

我有一个文本框window1.xaml,可用作搜索框。我在我的项目中使用集成的sql数据库,我有一个dataset.xsd以及tabel适配器和查询。

每当我需要查询数据库时,我都会这样做:

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

对于此搜索框,我有以下代码。

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;
 }

如何使用该命令查询我的数据库并填写dataTableBooks?

1 个答案:

答案 0 :(得分:0)

在此之后的几个小时里,我提出了这个解决方案。

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;
    }