sql - c# fill dataTable -


i have textbox window1.xaml works searchbox. using integrated sql database in project , have dataset.xsd tabel adapters , queries.

whenever need query database this:

bookstableadapter tableadapterbooks = new bookstableadapter(); datasetlibrary.booksdatatable datatablebooks; datatablebooks = tableadapterbooks.getdatabytitle(searchtext); 

for searchbox have 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 publishername books inner join publishers on books.codpublisher = publishers.codpublisher "; using (sqlcommand command = new sqlcommand()) {     (int = 0; < allwords.length; ++i)     {           if (i > 0)           sql += "or ";            string paramname = "@param" + i.tostring();           sql += string.format("(books.title {0}) ", paramname);           command.parameters.addwithvalue(paramname, allwords[i] + "%");      }      command.commandtext = sql;  } 

how can use command query database , fill datatablebooks?

after hours around this, have come 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 nomeeditora livros inner join editoras on livros.codeditora = editoras.codeditora ";         }         else         {             sql = "select livros.isbn, livros.titulo, livros.tema, livros.resumo, livros.imagem, livros.fototipo, editoras.nome nomeeditora 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 ";         }         using (command)         {             (int = 0; < allwords.length; ++i)             {                 if (i > 0)                 {                     sql += "or ";                 }                  if (searchoption == 1)                 {                     sql += string.format("(livros.titulo '%{0}%') ", allwords[i]);                 }                 else                 {                     sql += string.format("(livros.autor '%{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;     } 

Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

order - Notification for user in user account opencart -